ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
a = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
best = cur = a[0]
for x in a[1:]:
cur = max(x, cur + x)
best = max(best, cur)
print(best)- A. 4
- B. 6
- C. 12
- D. 1
Correct answer: B. 6
Kadane keeps the best sum of a subarray ending at the current index; the best run 4, −1, 2, 1 sums to 6. 4 is the largest single element, 12 adds every positive, and 1 is the whole-array sum.
More Python Programming questions
- What is printed? python print(sum(bin(i).count('1') for i in range(8)))
- What is printed? python seen = {} for i, x in enumerate([2, 7, 11, 15]): if 9 - x in seen: print((seen[9 - x], i)) break seen[x] = i
- What is printed? python print(len(bin(10 6)) - 2, len(bin(2 20)) - 2)
- What is printed? python print(max(range(10), key=lambda x: x % 4), min([], default=7))
- What is printed? python print(3 -1, 7 // 0.5, 2 0.5 2 0.5 == 2)
- What is printed? python print(float('%.2f' % 0.125), '%.3e' % 12345.678)