GateAI
Programming in C

GATE CSE 2016 Set 1 · Q34

MCQ2 marksMedium

Time

The following function computes the maximum value contained in an integer array p[] of size nn (n≥1n \ge 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

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: D) b != a

Programming in C

The algorithm uses two pointers, aa initialized to 00 and bb initialized to n−1n - 1, to narrow down the range containing the maximum element.

  • In each iteration, the elements p[a]p[a] and p[b]p[b] are compared:

    • If p[a]≤p[b]p[a] \le p[b], then p[a]p[a] cannot be the unique maximum, so index aa is incremented (a=a+1a = a + 1).

    • Otherwise (p[a]>p[b]p[a] > p[b]), p[b]p[b] cannot be the maximum, so index bb is decremented (b=b−1b = b - 1).

  • The search space decreases by 1 in each step until only a single element remains, which happens when a=ba = b.

  • After the loop terminates with a=ba = b, p[a]p[a] is returned, which contains the maximum value in the array.

Therefore, the loop must continue as long as a≠ba \neq b, which is given by the condition b != a.

Thus, (D) is the correct option.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

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

Where do you stand for GATE?

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.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Programming in C in GATE CSE

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 C

More from GATE CSE 2016 Set 1

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2016 Set 1 · Q34 · MCQ

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

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.