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<>
It does not works with above problemā¦
ok, sorry my bad , but this can solve the problem effeciently
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
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 .
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.
please accept the answer if u like this content.
All the best dear! I can feel your pain
@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???