Doubt regarding correctness of solution

I think this method might fail when array contains multiple entries for same value like {1,2,2,2,4,6} and we requires Key=6.

then by following method given in video,
Number of pairs = 1

Original answer, Number of pairs = 3.

What change is needed to correctly solve above case ?

hello @karan_bhut

it will include all distinct pairs. some repetetion might be missing.

extract all unique numbers also store their freuency.

now apply the same two pointer logic . but this if if a[i]+a[j]==target
then print a[i] and a[j] pairs n1 * n2 times where n1 and n2 are the frequency of a[i] and a[j]

we have to check if pair is already considered or not right ? eg: 1,2,2,4,4,6 will give 8 pairs for key 6. but answer is 4 right ?


u have to apply two pointers on unique numbers ,not on repeated array

Ohh okay understood.

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.