ProgrammingVery hardCognizant GenC · TCS NQT

What is printed?

What is printed?
def arrows(p):
    p = sorted(p, key=lambda x: x[1])
    cnt = 0
    end = None
    for s, e in p:
        if end is None or s > end:
            cnt += 1
            end = e
    return cnt
print(arrows([(10, 16), (2, 8), (1, 6), (7, 12)]), arrows([(1, 2), (3, 4), (5, 6), (7, 8)]))
  1. A. 3 4
  2. B. 2 3
  3. C. 2 4
  4. D. 1 4

Correct answer: C. 2 4

Sorting by right end and shooting whenever a balloon starts after the last arrow's end is the correct greedy. The first list needs 2 arrows, and four disjoint balloons need 4.

More Python Programming questions

Exams that ask this topic