GATE CS 2024 Set 2 — Question 42
Go beyond PYQs with Success TrackerAI-powered personalised practice and doubt support. Unlimited practice on eligible plans; AI usage limits apply.MCQ+2 / -0.67HardArrays & Strings in CC ProgrammingProgramming & Data StructuresArray Operations (1D, 2D)Arrays & Strings
Programming & Data Structures → Arrays & Strings → Array Operations (1D, 2D)
Last updated
Question
Consider an array that contains positive integers. A subarray of 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 that contains at most two distinct integers. The code has two missing expressions labelled and .Which one of the following options gives the CORRECT missing expressions?(Hint: At the end of the -th iteration, the value of
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];
}
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.)Correct answer
(B) (P) 1 (Q) len1+1
Solution
The code maintains two lengths:
1.
len1: The length of the current run of identical elements ending at .2.
Let's analyze the cases in the loop:len2: The length of the current subarray ending at containing at most two distinct elements.- Case 1:
len1 and len2 increment by 1. This is correctly handled in the code.- Case 2:
len2increments by 1 because it's still within the two-distinct-element limit.- Since , a new run of identical elements starts with . Thus,
len1must be reset to 1. So, . - The variable
secondis updated to the previousfirstto prepare for the next iteration. - Case 3: is a new distinct element
first nor second. To maintain at most two distinct elements, the new window must start from the beginning of the previous run of identical elements (first) and include the current element .- The length of this new window is the count of the previous identical elements (
len1) plus the current element. Thus,len2 = len1 + 1. So, . len1is reset to 1 for the new run starting at .
Continue learning with Success Tracker
A step still unclear? Work through it with support
Use Success Tracker to ask about the reasoning, then try another GATE CS question to check your understanding.
AI-powered practice· Unlimited practice on eligible plans
- PYQs with solutions
- Attempt available previous-year questions, then compare your reasoning with the worked solution. Coverage varies by stream.
- Practice that adapts
- Choose a topic, work on weaker areas and bookmark questions to revisit. Your attempts feed your progress tracking.
- AI doubt support
- Ask follow-up questions about a step or concept while practising, instead of stopping at the final answer.
Unlimited practice is available on eligible plans. Free practice and AI usage have limits; check the current plan allowances before choosing.
This page stays readable without an account. AI responses can be wrong; check them against the solution and source material.
More questions on Arrays & Strings
2026 Set 2 Q12The set T represents various traversals over binary tree. The set S represents the order of…2026 Set 2 Q19Consider the following three ANSI-C programs, P1, P2, and P3. P1 [code] P2 [code] **P3**…2026 Set 1 Q23Let be an odd number greater than 100. Consider a binary minheap with elements stored in an…2026 Set 1 Q24Consider a hash table that is initially empty. The hash table is maintained…2026 Set 1 Q27Consider the following C statements: Which of the following options is/are correct? [figure]