Data Structures & AlgorithmsModerateCodeforces Div2 C/D · Amazon SDE OA
Count the subarrays of w = [1, 3, 2, 5, 4, 4, 2] whose maximum minus minimum is at most 3. Run the brute-force count.
Count the subarrays of w = [1, 3, 2, 5, 4, 4, 2] whose maximum minus minimum is at most 3. Run the brute-force count.
w=[1,3,2,5,4,4,2] print(sum(1 for i in range(len(w)) for j in range(i,len(w)) if max(w[i:j+1])-min(w[i:j+1])<=3))What is printed?
- A. 11
- B. 24
- C. 15
- D. 17
Correct answer: B. 24
Each subarray is checked directly, including length-one subarrays. A tighter bound (at most 1 or at most 2) undercounts, and skipping single elements drops valid subarrays.
More Coding Interview Problems questions
- What is the minimum number of perfect squares (1, 4, 9, 16, ...) whose sum is exactly 12? Run the DP over all squares not exceeding 12…
- Count the subsequences (by position, not distinct strings) of "abcabc" that equal "abc". Run the DP that counts matches. python…
- Array a = [2, 3, 1, 1, 4, 2, 0, 3, 1, 1]; from index i you may jump to any index in [i+1, i+a[i]]. Find the minimum number of jumps from…
- Trapping rain water: heights h = [4, 2, 0, 3, 2, 5, 1, 0, 2]. Compute the total water trapped between the bars. python…
- The permutation p = [4, 3, 1, 7, 6, 2, 5] of 1..7 must be sorted by swaps of any two elements. What is the minimum number of swaps?…
- Count the lattice paths of length 8 with steps +1 and −1 that start at 0, never go below 0, and end at 0. Run the DP over positions and…