# 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ā¦

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.

3 Likes

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

1 Like