edited by
11,093 views
30 votes
30 votes

 The following function computes the maximum value contained in an integer array $P[ \ ]$ of size $n$ $(n>=1)$.                  

int max (int *p,int n) {
    int a = 0, b=n-1;
    
    while (__________) {
        if (p[a]<= p[b]) {a = a+1;}
        else             {b = b-1;}
    }
    return p[a];
}

The missing loop condition is:

  1.  $a\ \ != n$ 
  2.  $b\ \ != 0$ 
  3.  $b>(a+1)$ 
  4.  $b\ \ != a$ 
edited by

1 Answer

Best answer
60 votes
60 votes

Answer is (D).

Hint : Given in the question itself that we start comparing the contents of an array  from $a[0]$ and $a [n-1]$ (converging from both side) then condition must be till both meet at a point and that point will be $a=b$.
Hence loop condition should be $a!=b$.

Option C fails for $n=2, p = [1, 2].$

edited by
Answer:

Related questions