Gradient LabSign up free

Home / GATE DA / 2024 / Q.39

GATE DA 2024, Q.39: Output of computeS on [6, 3, 5, 4, 10]

ProgrammingArray pseudocode trace2 marksMCQFree

The question

computeS(X): S[1] ← 1; for i ← 2 to length(X): S[i] ← 1; if X[i−1] ≤ X[i] then S[i] ← S[i] + S[i−1]. Return S. What is returned for X = [6, 3, 5, 4, 10]?

computeS(X)
  S[1] ← 1
  for i ← 2 to length(X)
    S[i] ← 1
    if X[i−1] ≤ X[i]
      S[i] ← S[i] + S[i−1]
    end if
  end for
  return S

Watch it solved

Solve it step by step

  1. What S means. S[i] is the length of the non-decreasing run ending at position i.
  2. i = 2, 3. 3 < 6 so S = 1; 3 ≤ 5 so S = 2.
  3. i = 4. 4 < 5, run breaks: S = 1.
  4. i = 5. 4 ≤ 10: S = 1 + 1 = 2. Result [1, 1, 2, 1, 2].

Answer: (C) [1, 1, 2, 1, 2]

Official IISc Bengaluru answer key: C.

Common mistake. Treating S like a "longest increasing subsequence" (giving 3 or 4 at the end). The run resets whenever the sequence drops.

Remember. S[i] counts the current non-decreasing streak; any drop resets it to 1.

Want every question solved? The past-paper pack has all 195 questions from 2024, 2025 and 2026 for ₹499. The cheat sheet pack has 23 printable sheets for ₹399.

See the past-paper pack See the cheat sheets

Source: official GATE DA 2024 question paper and answer key (IISc Bengaluru). The question is lightly reworded; every number and option is kept.