My doubt is related to the two pointer algorithm where we have to print out the no. of pairs of elements in an array that have sum equal to K (where K is an integer).
To solve this, one part of the solution says that - If the sum is bigger than the target sum, this means that we need a smaller sum so, we are going to decrement the Pointer 2 (end-pointer).
My doubt is - when the sum is greater than K, it’s understandable that we have to decrement the Pointer 2(end-pointer) to get close to K but WHY CAN’T WE DECREASE POINTER-1? Decreasing Pointer-1 should also get close to K, isn’t it?
I have seen dozens of solution/explanation on two pointer algorithm and couldn’t find an answer as to why we don’t decrease Pointer-1. Need your help.