Data Structures & AlgorithmsModerateCodeforces Div2 C/D · Amazon SDE OA
Array b = [9, 2, 7, 4, 1, 8, 3]. Count the pairs (i < j) with b[i] > 2·b[j] (a reverse pair). Run the brute-force code.
Array b = [9, 2, 7, 4, 1, 8, 3]. Count the pairs (i < j) with b[i] > 2·b[j] (a reverse pair). Run the brute-force code.
b=[9,2,7,4,1,8,3] print(sum(1 for i in range(len(b)) for j in range(i+1,len(b)) if b[i]>2*b[j]))What is printed?
- A. 13
- B. 9
- C. 8
- D. 4
Correct answer: C. 8
A reverse pair needs the strict inequality b[i] > 2·b[j] with i before j. Allowing equality over-counts, plain inversions count a different set, and reversing the index roles checks the wrong direction.
More Coding Interview Problems questions
- For the array c = [1, 2, 3, 4], consider every non-empty subarray and its XOR value. How many subarrays have an XOR value that is strictly…
- For the string "character", find the length of its longest palindromic subsequence (characters need not be adjacent, but must keep their…
- 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. python…
- 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…