Data Structures & AlgorithmsVery hardGoogle · Amazon coding round
How many pairs of indices (i, j) with i < j satisfy a[i] > 2·a[j] in a = [5, 2, 6, 1, 7, 3, 9, 2, 4]?
- A. 9
- B. 16
- C. 12
- D. 7
Correct answer: A. 9
Counting pairs with a[i] > 2·a[j] directly (or by a merge-sort-based count taken before merging) gives 9. The plain inversion count is 16, which ignores the factor 2.
More Coding Interview Problems questions
- A walker moves from (0,0) to (4,4) on lattice points using steps (1,0), (0,1) and (1,1). The point (2,2) is forbidden. How many different…
- How many permutations of {1, 2, …, 8} have exactly two fixed points (positions i with π(i) = i)?
- How many integers n with 1 ≤ n ≤ 999 have digit sum exactly 12 and contain no digit 0 (in their usual decimal form, without leading zeros)?
- Find the length of the longest common subsequence of X = "AGGTABQXZRYWM" and Y = "GXTXAYBZQMWRY".
- What is the minimum number of characters that must be inserted anywhere in "CBAABDCABBDAC" to make it a palindrome?
- Matrices A1…A7 have dimensions 5×10, 10×3, 3×12, 12×5, 5×50, 50×6, 6×8. What is the minimum number of scalar multiplications needed to…