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:
If option C was b >= (a+1), then it would have worked!
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].$
Above loop give correct answer when loop condition is :-
but it gives wrong result on :
64.3k questions
77.9k answers
244k comments
80.0k users