ProgrammingVery hardCodeforces-style · placement coding round
What is printed?
What is printed?
def div_count(a, k):
cnt = {0: 1}
s = 0
ans = 0
for x in a:
s = (s + x) % k
ans += cnt.get(s, 0)
cnt[s] = cnt.get(s, 0) + 1
return ans
print(div_count([4, 5, 0, -2, -3, 1], 5), (-7) % 3)- A. 7 -3
- B. 7 2
- C. 5 2
- D. 7 1
Correct answer: B. 7 2
Prefix sums taken modulo k with Python's % stay in 0 to k-1 even for negative numbers, so equal remainders mark a subarray whose sum is divisible by k. The example gives 7, and (-7) % 3 is 2.
More Python Programming questions
- What is printed? python print(sorted(['b', 'A', 'a', 'B']), sorted(['b', 'A', 'a', 'B'], key=str.lower))
- What is printed? python print('a-b-c-d'.rsplit('-', 2), 'a-b-c-d'.split('-', 1))
- What is printed? python print('²'.isdigit(), '²'.isdecimal(), '٣'.isdecimal())
- What is printed? python m = ['abc', 'def'] print([''.join(t) for t in zip(m)])
- What is printed? python print(list(zip([[1, 2, 3], [4, 5]])), list(zip([[1, 2], [3, 4, 5]], strict=False)))
- What is printed? python from collections import deque a = [1, 3, -1, -3, 5, 3, 6, 7] k = 3 dq = deque() out = [] for i, x in enumerate(a)…