Replace all elements of array by its square.and sort it
Now question is to find two  nos in the array such that their sum is N.
For this take two pointers min and max
Initialize min to 0 and max to n-1(n-size of array)
1.find the sum a[min]+a[max]
2.if sum>N max=max-1
if sum<N min=min+1
if sum==N we have a sol

Now in case of nonunique values all possible pairs must be displayed
eg for 2 2 3 3 N =5
min=0,max 3 is a sol but u need to display all combination of 2 and 3 for
that i used tempmin=position till which we have a value a[min] and temp max
postion till which we have a value a[max] and display
all possible combinations.

P.S. This was asked to me in Amazon

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to