What if there are more pairs for same numbers

If my array is

int a[7] = { 1 ,1 ,3 ,7,7, 9 , 10 };

given sum is 8

we should get four pairs

index 0 , index 4
index 0 , index 3
index 1 , index 4
index 1 , index 4

How to solve this problem?

Hey @stathe
A simple solution is be traverse each element and check if there’s another number in the array which can be added to it to give sum.If pair satisfy sum then print it.

Second method is using hashmap
We create an empty hash table. Now we traverse through the array and check for pairs in hash table. If a matching element is found, we print the pair number of times equal to the number of occurrences of the matching element.
Note that the worst case of time complexity of this solution is O(c + n) where c is count of pairs with given sum.

If u don’t know what hasmap is then stick to 1st soln

Hey @stathe
Is this resolved ?