ProgrammingHardAmazon OA · Flipkart coding round
What is printed?
What is printed?
def kadane(a):
best = cur = a[0]
start = best_l = best_r = 0
for i in range(1, len(a)):
if cur < 0:
cur = a[i]
start = i
else:
cur += a[i]
if cur > best:
best = cur
best_l, best_r = start, i
return best, best_l, best_r
print(kadane([-2, 1, -3, 4, -1, 2, 1, -5, 4]))- A. (6, 2, 6)
- B. (4, 3, 3)
- C. (6, 3, 5)
- D. (6, 3, 6)
Correct answer: D. (6, 3, 6)
Kadane's rule restarts the running sum when it turns negative and records the bounds of the best sum. The best subarray is 4, -1, 2, 1 with sum 6, from index 3 to index 6.
More Python Programming questions
- What is printed? python def threesum(a): a = sorted(a) out = [] for i in range(len(a) - 2): if i and a[i] == a[i - 1]: continue l, r = i +…
- What is printed? python def distinct(s, t): dp = [1] + [0] len(t) for ch in s: for j in range(len(t), 0, -1): if t[j - 1] == ch: dp[j] +=…
- What is printed? python import bisect def pairsle(a, d): a = sorted(a) c = 0 for i, x in enumerate(a): j = bisect.bisectright(a, x + d, i…
- What is printed? python import bisect def closest(a, x): i = bisect.bisectleft(a, x) cands = a[max(i - 1, 0):i + 1] return min(cands…
- What is printed? python def content(g, s): g.sort(); s.sort() i = j = 0 while i < len(g) and j < len(s): if s[j] >= g[i]: i += 1 j += 1…
- What is printed? python from collections import Counter def least(tasks, n): c = Counter(tasks) m = max(c.values()) k = sum(1 for v in…