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]))- A. 4 2
- B. 3 3
- C. 5 3
- 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
- What is printed? python def pi(s): p = [0] len(s) k = 0 for i in range(1, len(s)): while k and s[i] != s[k]: k = p[k - 1] if s[i] == s[k]…
- What is printed? python from collections import deque def bip(g): col = {} for s in g: if s in col: continue col[s] = 0 q = deque([s])…
- What is printed? python def mst(n, edges): p = list(range(n)) def f(x): while p[x] != x: x = p[x] return x total = 0 for w, u, v in…
- What is printed? python def candy(r): n = len(r) c = [1] n for i in range(1, n): if r[i] > r[i - 1]: c[i] = c[i - 1] + 1 for i in range(n…
- What is printed? python def mat(a, b, m): return [[sum(a[i][k] b[k][j] for k in range(2)) % m for j in range(2)] for i in range(2)] def…
- What is printed? python class A: x = 1 def get(self): return self.x a = A() a.x = 5 A.x = 9 b = A() print(a.get(), b.get(), A.x)