I read Find two elements in an array that sum to k and How can I find two elements in an array that sum to k as they are related.
I know a O(n) solution but i see that a O(n logn) also exists :-
p=0,q=n-1;
while(p<q)
{
if(a[p]+a[q]==k)
{
cout<<a[p]<<"\t"<<a[q];
p++;
q--;
}
else if(a[p]+a[q]>k)
q--;
else
p++;
}
This requires the array to be first sorted. But since the values of p and q are dependent on the elements in the array, how do we assert that the complexity of this algorithm is O(n Log n)?