ProgrammingVery hardCodeforces-style · placement coding round

What is printed?

What is printed?
def jumps(a):
    j = cur = far = 0
    for i in range(len(a) - 1):
        far = max(far, i + a[i])
        if i == cur:
            j += 1
            cur = far
    return j
print(jumps([2, 3, 1, 1, 4]), jumps([2, 3, 0, 1, 4]))
  1. A. 2 3
  2. B. 1 2
  3. C. 2 2
  4. D. 3 2

Correct answer: C. 2 2

Greedy BFS-style jumping adds a jump each time the current reach is used up, then extends the reach to the farthest index seen. Both arrays need exactly 2 jumps.

More Python Programming questions

Exams that ask this topic