in Algorithms
367 views
0 votes
0 votes
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$.
in Algorithms
367 views

1 Answer

0 votes
0 votes
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;

}

Related questions