GATE CS 2024 Set 2 | Question 42

Programming & Data Structures › Arrays & Strings · MCQ · 2 marks · Answer: B

  1. Question 42Programming & Data Structuresmcq2 marks
    A 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 When X[i] equals the second-most-recent distinct value (the else if branch), the run of equal values restarts at this element, so len1 = 1 (P). When X[i] is a brand-new third value (the else branch), the at-most-two-distinct window can only extend the previous equal-run plus this element, so len2 = len1+1 (Q).

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 →