ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
def peak(a):
lo, hi = 0, len(a) - 1
while lo < hi:
mid = (lo + hi) // 2
if a[mid] < a[mid + 1]:
lo = mid + 1
else:
hi = mid
return lo
print(peak([1, 3, 8, 12, 4, 2]), peak([10, 9, 8]))- A. 2 0
- B. 3 0
- C. 3 2
- D. 4 0
Correct answer: B. 3 0
The comparison with the next item shows whether the peak lies to the right. 12 is the peak at index 3 of the first list, and a strictly falling list peaks at index 0.
More Python Programming questions
- What is printed? python from functools import lrucache @lrucache(maxsize=None) def f(e, n): if n == 0: return 0 if e == 1: return n lo, hi…
- What is printed? python def lal(a): dp = [dict() for in a] best = 0 for i in range(len(a)): for j in range(i): d = a[i] - a[j] dp[i][d] =…
- What is printed? python def pi(s): p = [0] len(s) k = 0 for i in range(1, len(s)): while k and s[i] != s[k]: k = p[k - 1] if s[i] == s[k]…
- What is printed? python from collections import deque def bip(g): col = {} for s in g: if s in col: continue col[s] = 0 q = deque([s])…
- What is printed? python def mst(n, edges): p = list(range(n)) def f(x): while p[x] != x: x = p[x] return x total = 0 for w, u, v in…
- What is printed? python def candy(r): n = len(r) c = [1] n for i in range(1, n): if r[i] > r[i - 1]: c[i] = c[i - 1] + 1 for i in range(n…