GATE CS 2024 Set 2 — Question 42
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.)A.
len1+1 len2+1B.
1 len1+1C.
1 len2+1D.
len2+1 len1+1Correct 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 .
More questions on Arrays & Strings
2024 Set 2 Q13Consider the following C program. Assume parameters to a function are evaluated from right to left.…2024 Set 2 Q17Let be the adjacency matrix of a simple undirected graph . Suppose is its own inverse.…2024 Set 1 Q17Given an integer array of size , we want to check if the array is sorted (in either ascending or…2024 Set 1 Q18Consider the following C program: [code] Which one of the following statements is CORRECT?2024 Set 1 Q19Consider the following C program: [code] Assume that the input to the program from the command line…
Practice GATE CS PYQs with adaptive difficulty
Timed practice, skill tracking, and AI explanations — free to start.
Start practicing free