in Programming in C edited by
11,160 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$ 
in Programming in C edited by
11.2k views

4 Comments

option (a) getting out itself as a! = n would not be the case always. for e.g. take n=10 then no possibl value of a for which a! =10.

option (b) out as b! =0 never be the case.

option (c) getting out as there may be atleast one element which may not be compared.
0
0
i took {8,10,2,1,9}array ,i am taking first option but didnt get any logic .
0
0

If option C was b >= (a+1), then it would have worked!

10
10
@once_2019 why are thinking about factorial in this question 1st and 2nd option gave only for distraction
0
0

1 Answer

59 votes
59 votes
Best answer

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

4 Comments

@srestha Mam so what is the final answer,as per your explanation?

b>a+1 it gives wrong answer and for b>a it gives correct answer????

am I right?
0
0
Yes it’s correct, in that case also loop terminates when b=a 🙂
0
0
edited by

Above loop give correct answer when loop condition is :-

  • b >= (a+1)
  • b > a

but it gives wrong result on :

  • b > (a+1)
  • b >= a
5
5
Answer:

Related questions