Data Structures & AlgorithmsModerateCodeforces Div2 C/D · Amazon SDE OA
Find the sum of the maximum of every window of length 3 in a = [1, 3, −1, −3, 5, 3, 6, 7] (windows slide one step at a time).
Find the sum of the maximum of every window of length 3 in a = [1, 3, −1, −3, 5, 3, 6, 7] (windows slide one step at a time).
a=[1,3,-1,-3,5,3,6,7] print(sum(max(a[i:i+3]) for i in range(len(a)-2)))What is printed?
- A. 22
- B. -4
- C. 26
- D. 29
Correct answer: D. 29
Each window contributes its maximum; a monotonic deque gives the same numbers in linear time. Window minima, windows of length four, and dropping the final window each change the sum.
More Coding Interview Problems questions
- In the reflected binary Gray code, the number with index i is i XOR (i >> 1). Using 0-based indices, what is the value printed for i = 5…
- How many binary strings of length 30 contain neither the substring "11" nor the substring "000"? python n=30 dp={('0',1):1,('1',1):1} for…
- How many primes p satisfy p ≤ 1,000,000 and p ≡ 3 (mod 4)? python N=106 s=bytearray([1])(N+1); s[0]=0; s[1]=0 for i in…
- The recursive extended Euclidean algorithm eg(a, b) returns (g, x, y) with a·x + b·y = g, using eg(a, b) = (g, y, x − (a//b)·y) when b ≠ 0…
- Matrix chain: matrices A1..A6 have dimension sequence p = [30, 35, 15, 5, 10, 20, 25] (Ai is p[i−1] × p[i]). What is the minimum number of…
- Game of two eggs and 100 floors: you have two identical eggs and must find the highest floor from which a dropped egg survives, using as…