ProgrammingHardAmazon OA · Flipkart coding round
What is printed?
What is printed?
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 in range(m):
P[i+1][j+1] = g[i][j] + P[i][j+1] + P[i+1][j] - P[i][j]
def rect(r1, c1, r2, c2):
return P[r2+1][c2+1] - P[r1][c2+1] - P[r2+1][c1] + P[r1][c1]
print(rect(1, 1, 2, 2), rect(0, 0, 0, 2))- A. 27 6
- B. 28 6
- C. 28 12
- D. 24 6
Correct answer: B. 28 6
The 2D prefix table answers a rectangle with four corner lookups. The bottom-right 2x2 block is 5+6+8+9 = 28, and the top row is 1+2+3 = 6.
More Python Programming questions
- 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…
- What is printed? python def cool(p): hold, sold, rest = float('-inf'), 0, 0 for x in p: hold, sold, rest = max(hold, rest - x), hold + x…