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] The missing loop condition is
Topic-wise GATE CS PYQs with verified steps
GATE CSE 2016 Set 1 · Q34
Time
p[] of size ().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 isSelect one answerSingle correct
Solution
Official answer: D) b != a
Programming in CThe algorithm uses two pointers, initialized to and initialized to , to narrow down the range containing the maximum element.
In each iteration, the elements and are compared:
If , then cannot be the unique maximum, so index is incremented ().
Otherwise (), cannot be the maximum, so index is decremented ().
The search space decreases by 1 in each step until only a single element remains, which happens when .
After the loop terminates with , is returned, which contains the maximum value in the array.
Therefore, the loop must continue as long as , which is given by the condition b != a.
Thus, (D) is the correct option.
Try the question first, then check your work.
Examiner trap
The two pointers approach each other by eliminating one non-maximum element at each iteration until both pointers point to the exact same element.
This Programming in C previous year question is Q34 of the GATE CSE 2016 Set 1 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, or browse all GATE CSE PYQs.
Free diagnostic
Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Programming in C PYQs now with no account.
An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.
Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 19 of them are 2-mark MCQs like this one, and GATE CSE 2016 Set 1 has 3.
How GATE tests Programming in CIndependent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.