ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
def cnt(a, k):
p, l, res = 1, 0, 0
for r, x in enumerate(a):
p *= x
while l <= r and p >= k:
p //= a[l]
l += 1
res += r - l + 1
return res
print(cnt([10, 5, 2, 6], 100), cnt([1, 2, 3], 0))- A. 8 0
- B. 8 3
- C. 7 0
- D. 6 0
Correct answer: A. 8 0
This is a sliding window over subarrays whose product stays below k. For [10,5,2,6] with k=100 the windows add up to 8, and with k=0 no window qualifies, so the result is 0.
More Python Programming questions
- What is printed? python g = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] n, m = 3, 3 P = [[0] (m + 1) for in range(n + 1)] for i in range(n): for j…
- What is printed? python def splits(a): total = sum(a) s = 0 c = 0 for x in a[:-1]: s += x if 2 s == total: c += 1 return c…
- What is printed? python from collections import deque def zeroone(n, edges, src): adj = [[] for in range(n)] for u, v, w in edges…
- What is printed? python from collections import deque def ladder(begin, end, words): S = set(words) q = deque([(begin, 1)]) seen = {begin}…
- What is printed? python def can(a): s = sum(a) if s % 2: return False t = s // 2 dp = [False] (t + 1) dp[0] = True for x in a: for v in…
- What is printed? python def paths(g): R, C = len(g), len(g[0]) dp = [[0] C for in range(R)] dp[0][0] = 0 if g[0][0] else 1 for r in…