ProgrammingVery hardCodeforces-style · placement coding round

What is printed?

What is printed?
def lal(a):
    dp = [dict() for _ in a]
    best = 0
    for i in range(len(a)):
        for j in range(i):
            d = a[i] - a[j]
            dp[i][d] = dp[j].get(d, 1) + 1
            best = max(best, dp[i][d])
    return best
print(lal([3, 6, 9, 12]), lal([9, 4, 7, 2, 10]))
  1. A. 4 2
  2. B. 3 3
  3. C. 5 3
  4. D. 4 3

Correct answer: D. 4 3

dp[i][d] stores the length of the longest arithmetic run ending at index i with common difference d. The first list is 3, 6, 9, 12 with difference 3, and the second contains 4, 7, 10 with difference 3.

More Python Programming questions

Exams that ask this topic