GateAI
Programming in C

GATE CSE 2024 Set 2 · Q42

MCQ2 marksMedium

Time

Consider an array XX that contains nn positive integers. A subarray of XX is defined to be a sequence of array locations with consecutive indices. The C code snippet given below has been written to compute the length of the longest subarray of XX that contains at most two distinct integers. The code has two missing expressions labelled (P)(P) and (Q)(Q).int first=0, second=0, len1=0, len2=0, maxlen=0; for (int i=0; i < n; i++) { if (X[i] == first) { len2++; len1++; } else if (X[i] == second) { len2++; len1 = (P) ; second = first; } else { len2 = (Q) ; len1 = 1; second = first; } if (len2 > maxlen) { maxlen = len2; } first = X[i]; }Which one of the following options gives the CORRECT missing expressions? (Hint: At the end of the ii-th iteration, the value of len1 is the length of the longest subarray ending with X[i]X[i] that contains all equal values, and len2 is the length of the longest subarray ending with X[i]X[i] that contains at most two distinct values.)

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: B) (P) 1 (Q) len1+1

Programming in C

At each iteration ii:

  • first stores the value of the immediately preceding element, X[i−1]X[i-1].

  • second stores the other distinct integer present in the current two-distinct-element window.

  • len1 tracks the length of the continuous run of identical elements ending at X[i]X[i].

  • len2 tracks the length of the longest contiguous subarray ending at X[i]X[i] with at most 2 distinct values.

Let us analyze the three branches:

  1. Case 1 (X[i] == first): X[i]X[i] extends the current streak of identical elements as well as the 2-distinct window. Both len1 and len2 increment by 1.

  2. Case 2 (X[i] == second): X[i]X[i] is one of the two allowed values, so the 2-distinct window extends (len2++). However, since X[i]≠firstX[i] \neq \text{first}, a new continuous run of identical elements starts with this single element X[i]X[i]. Therefore, the new len1 must be initialized to 11. Hence, (P)=1(P) = 1.

  3. Case 3 (else): A third distinct element is encountered. The new valid 2-distinct subarray ending at X[i]X[i] can only consist of the trailing contiguous run of first elements (whose count is stored in len1 from the previous iteration) plus the new element X[i]X[i]. Thus, the new len2 becomes len1+1\text{len1} + 1. Hence, (Q)=len1+1(Q) = \text{len1} + 1.

Therefore, (P)=1(P) = 1 and (Q)=len1+1(Q) = \text{len1} + 1, which corresponds to Option (B).

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 Q42 of the GATE CSE 2024 Set 2 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 2024 Set 2 has 4.

How GATE tests Programming in C

More from GATE CSE 2024 Set 2

Share this solution

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

GateAI solution cardGATE CSE 2024 Set 2 · Q42 · MCQ

Consider an array X that contains n positive integers. A subarray of X is defined to be a sequence of array locations with consecutive indices. The C code snippet given below has been written to compute the length of the longest subarray of X that contains at most two distinct integers. The code has two missing expressions labelled (P) and (Q). int first=0, second=0, len1=0, len2=0, maxlen=0; for (int i=0; i maxlen) { maxlen = len2; } first = X[i]; } Which one of the following options gives the CORRECT missing expressions? (Hint: At the end of the i-th iteration, the value of len1 is the length of the longest subarray ending with X[i] that contains all equal values, and len2 is the length of the longest subarray ending with X[i] that contains at most two distinct values.)

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.