Data Structures & AlgorithmsVery hardProduct Company SDE · ICPC regional
Compute the sum over all subarrays of the minimum element, for a = [3, 1, 2, 1, 3]. Subarrays are counted by position, so equal values in different positions are separate subarrays.
Compute the sum over all subarrays of the minimum element, for a = [3, 1, 2, 1, 3]. Subarrays are counted by position, so equal values in different positions are separate subarrays.
a=[3,1,2,1,3] print(sum(min(a[i:j+1]) for i in range(len(a)) for j in range(i,len(a))))What is printed?
- A. 10
- B. 37
- C. 20
- D. 23
Correct answer: C. 20
For each element count the subarrays where it is the minimum, strict on one side and non-strict on the other so equal values are not double counted. Summing maxima answers a different question, and dropping length-one subarrays removes single elements.
More Coding Interview Problems questions
- 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). python…
- 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…