GATE CS 2016, set 1 · Question 44 · Programming and Data Structures · Multiple choice (MCQ) · 2 marks · −2/3 for a wrong answer

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) a != n
  2. (B) b != 0
  3. (C) b > (a + 1)
  4. (D) b != a
Answer

Answer (D)

Answer from the official GATE CS 2016 answer key.

The step-by-step solution, where available, is part of GATE CS Pro. See what Pro includes