edited by
421 views
1 votes
1 votes

Given an array S containing n real numbers, and a real number x. We want to find any two elements p and q in the array such that their sum is greater than the real number x. What is the best possible time complexity to find p and q ?

  1. $O(1)$
  2. $O(n^2 \log n)$
  3. $O(n)$
  4. $O(n \log n)$

Question asks about Best case time complexity . So , if it happens , in the first and second element's sum is greater than x , then it can be done in O(1) time , right ?

Please correct me.

edited by

1 Answer

Best answer
5 votes
5 votes
No, in question, best time complexity means best algorithm i.e. what is worst case time complexity of best algorithm.

I think the answer is $O(n)$.

We can find largest and second largest numbers in $S$ in $O(n)$ time. Now see if their sum is greater than $x$, if it is, return these two numbers, otherwise there can't be any pair whose sum is greater than $x$.
selected by

Related questions

0 votes
0 votes
1 answer
2
gshivam63 asked May 21, 2016
471 views
find(int n) { if(x<2)return; else for(i =1; i<=4; i++) find (n/2); for(i= 1;i< =n*n; i++) sum=sum+1; }
2 votes
2 votes
2 answers
3
Amar Vashishth asked Aug 2, 2015
2,446 views
int fun(int n) { int count=0; for (int i= n; i 0; i/=2) for(int j=0; j< i; j++) count+= 1; return count; }
8 votes
8 votes
2 answers
4