ProgrammingVery hardAmazon OA · Flipkart coding round
What is printed?
What is printed?
def three_less(a, t):
a = sorted(a)
n = len(a)
c = 0
for i in range(n - 2):
l, r = i + 1, n - 1
while l < r:
if a[i] + a[l] + a[r] < t:
c += r - l
l += 1
else:
r -= 1
return c
print(three_less([-2, 0, 1, 3], 2))- A. 1
- B. 3
- C. 2
- D. 4
Correct answer: C. 2
Fix the first index and move two pointers. When a sum is below target, every index between left and right also works, so the code adds r - l at once. For [-2,0,1,3] and target 2 that gives 2.
More Python Programming questions
- What is printed? python import heapq def rooms(iv): iv = sorted(iv) h = [] for s, e in iv: if h and h[0] <= s: heapq.heapreplace(h, e)…
- For a Python dict d with n entries, which operation costs O(n) rather than O(1) on average?
- With n = 2 x 10^5 and a 1-second limit in CPython, which algorithm can be rejected because it is too slow?
- A solution loops over 2 x 10^5 queries, and each query runs if x in lst on a list of 2 x 10^5 items. Which change keeps the same answers…
- What is printed? python def tri(t): dp = t[-1][:] for row in range(len(t) - 2, -1, -1): for j in range(len(t[row])): dp[j] = t[row][j] +…
- What is printed? python def lps(s): n = len(s) dp = [[0] n for in range(n)] for i in range(n): dp[i][i] = 1 for L in range(2, n + 1): for…