371 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

0 votes
0 votes
0 answers
1
akash.dinkar12 asked Jun 26, 2019
217 views
Give an algorithm that determines the number of inversions in any permutation on $n$ elements in $\Theta (n\ lg\ n)$ worst-case time. (Hint: Modify merge sort.)
1 votes
1 votes
1 answer
2
akash.dinkar12 asked Jun 27, 2019
422 views
Argue that for any constant $0<\alpha\leq 1/2$, the probability is approximately $1-2\alpha$ that on a random input array, PARTITION produces a split more balanced than $...
0 votes
0 votes
0 answers
4