Show all pairs with given sum

no it only count 1 pair 2,4 which is correct

You are wrong. It’s complexity will be O(nlogn). It’s map<> not a unordered_map<>

1 Like

It does not works with above problem…

ok, sorry my bad , but this can solve the problem effeciently

code is working…ur problem solution is here-- YouTube

this will clear ur all doubts

dear, can you explain that “4 6”? If I am getting the Q correctly, we need to print the pairs, right? Shouldn’t it be “4 2” and “2 4”? Or am I missing something? Also, is there any constraint/info regarding order in which pairs must be printed??

Please tell the logic of this code…

He mentioned in comments that he already read it. But he would appreciate if you can give some solution from your side :slight_smile:

Nice tutorial …Tnq @vivek96

1 Like

first loop : First store all the elements of the array with key/value pair (map table).
second loop : take a element from the map table (say x) , then find the other element (say y) such that y==(sum-x) using FIND function.
If found print both element i.e, X and Y .

1 Like

is it possible in O(nlogn)?

if you want to print all unique pairs of numbers, then solutions given here are mostly ok, but if you want to print all pairs of indices (i, j) for which a[i] + a[j] = k, where k is your sum, then there is no way to do this in general faster than O(n^2), because there are O(n^2) such pairs of indices.


Sure. Have mid-sem in one hour. Will give by today.

1 Like

please accept the answer if u like this content.

All the best dear! I can feel your pain :stuck_out_tongue:

@pkacprazak - Can we not just add another line to print indices (j,i) along with (i,j)? Will it still be inefficient?

If i want to find the unique output …Then what is change in this code???