12 views
Describe a $\Theta(n\ lg\ n)$ time algorithm that, given a set $S$ of $n$ integers and another integer $x$, determines whether or not there exist two elements in $S$ whose sum is exactly $x$.
| 12 views

Sort the elements - Complexity $\theta\large{(nlogn)}$

Set two pointers, low=array[1] , high=array[n]

func(check)

{

while(low!=high)

{

if(array[low]+array[high]>x)

{

high=high-1;

}

else

if(array[low]+array[high]<x)

{

low=low+1;

}

else

return true;

}

return false;

}
by Active (2.4k points)