Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div2 D
Problem: In the array [5, 1, 4, 2, 8, 0, 2], count the pairs (i, j) with i < j and a[i] > 2·a[j] (so a[i] is more than double a[j]).
- A. 11
- B. 12
- C. 9
- D. 4
Correct answer: C. 9
Brute force over i < j finds 9 pairs with a[i] > 2·a[j] (e.g. 5 > 2·1, 5 > 2·0, 4 > 2·0, 8 > 2·2). Using ≥ adds the equality cases for 11, plain inversions a[i] > a[j] give 12, and reversing the roles (2·a[i] < a[j]) gives 4.
More Coding Interview Problems questions
- Problem: The string "aabcbbca" is processed by a sliding window that keeps at most one distinct letter 'a' inside (other letters are…
- Problem: How many integers in [1, 200] are divisible by exactly one of 6 and 8?
- Problem: The Euler's criterion test: count the quadratic residues modulo 13 among 1 through 12 (a residue r satisfies x² ≡ r for some…
- Problem: How many 4-digit numbers (1000 to 9999) have all four digits distinct and in strictly decreasing order from left to right?
- Problem: Compute the sum of the digits of the number 2^20 × 5^18 written in decimal.
- Problem: Let f(n) = number of 1-bits in n. Compute Σ f(n) for n from 0 to 63 inclusive.