Consider the function computeS(X) whose pseudocode is given below: Which ONE…

2024

Consider the function computeS(X) whose pseudocode is given below:

Which ONE of the following values is returned by the function computeS(X) for X = [6, 3, 5, 4, 10]?

  1. A.

    [1, 1, 2, 3, 4]

  2. B.

    [1, 1, 2, 3, 3]

  3. C.

    [1, 1, 2, 1, 2]

  4. D.

    [1, 1, 2, 1, 5]

Attempted by 761 students.

Show answer & explanation

Correct answer: C

Apply the pseudocode to X = [6, 3, 5, 4, 10].

  1. i = 1: S[1] = 1 (initial assignment).

  2. i = 2: Start with S[2] = 1. Compare X[1] = 6 and X[2] = 3. Since 6 <= 3 is false, leave S[2] = 1.

  3. i = 3: Start with S[3] = 1. Compare X[2] = 3 and X[3] = 5. Since 3 <= 5 is true, set S[3] = 1 + S[2] = 2.

  4. i = 4: Start with S[4] = 1. Compare X[3] = 5 and X[4] = 4. Since 5 <= 4 is false, leave S[4] = 1.

  5. i = 5: Start with S[5] = 1. Compare X[4] = 4 and X[5] = 10. Since 4 <= 10 is true, set S[5] = 1 + S[4] = 2.

Final returned S = [1, 1, 2, 1, 2].

Explore the full course: Gate Guidance By Sanchit Sir