Two Pointers Approach

If we want to find the pairs whose sum is equal to k by two pointer’s approach .how can we find when the array is
int a[]={1,1,1,1} and sum is 2.
total number of pairs is 6.

hello @Kumarrituranjan2056

u cannot use two pointers to find number of ways ( if number repeats).
use hasing or binary search to find number of such pairs.

Okay so this is one possible approach. But you would have to make some implementation tweaks to accomodate the conditions as mentioned by the previous TA.

Sort the array.
lets say there are n numbers. Remove all those numbers which are greater than n (assuming every number is positive), lets say the reduced array has N numbers.
Take a left pointer as index N/2 and right pointer as index N/2+1.
Now you can move the left pointer towards left and right pointer towards right to find the suitable pair.
Try proving this its easy.

But then again you should use binary search to solve this question mentioned by the previous TA

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.