GATE CS 2024 Set 2 | Question 42
Programming & Data Structures › Arrays & Strings · MCQ · 2 marks · Answer: B
- Question 42Programming & Data Structuresmcq2 marksA C snippet computes the longest subarray with at most two distinct integers. len1 = longest suffix of equal values, len2 = longest suffix with <=2 distinct values. Fill (P) and (Q):
- A.(P) len1+1 (Q) len2+1
- B.(P) 1 (Q) len1+1✓
- C.(P) 1 (Q) len2+1
- D.(P) len2+1 (Q) len1+1
Answer: B
Solution
Answer: (B) (P) 1 (Q) len1+1 WhenX[i]equals the second-most-recent distinct value (theelse ifbranch), the run of equal values restarts at this element, solen1 = 1(P). WhenX[i]is a brand-new third value (theelsebranch), the at-most-two-distinct window can only extend the previous equal-run plus this element, solen2 = len1+1(Q). - A.
More GATE PYQ on Arrays & Strings
All GATE CS Programming & Data Structures PYQ →Attempt GATE CS 2024 Set 2 in real GATE exam mode
Full paper with a timer, the GATE interface and a topic-wise analysis — free.
Create free account →