Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div2 B
Problem: How many pairs i < j in the array [2, 8, 9, 18, 4, 32, 50, 72] have a product a[i] × a[j] that is a perfect square?
- A. 24
- B. 16
- C. 32
- D. 1
Correct answer: B. 16
Split by square-free part: 2, 8, 18, 32, 50, 72 all equal 2·k² (C(6, 2) = 15 pairs) and 9, 4 are squares (1 pair), so 16. Letting j start at i adds the 8 self-products (24), counting ordered pairs gives 32, and requiring both factors to be squares gives 1.
More Coding Interview Problems questions
- Problem: How many three-digit numbers (100 to 999) have strictly increasing digits from left to right (for example 147 qualifies, 151 does…
- Array a = [1, 2, 3, 4, -1, 5, 0, 2]. How many contiguous subarrays have a sum of exactly 10?
- Array a = [2, -2, 3, -3, 4, -4, 1]. How many DISTINCT values appear among the prefix sums P[0], P[1], ..., P[7] (including P[0] = 0)?
- Sorted arrays A = [3, 8, 12, 17, 25] and B = [5, 9, 14, 20]. Choose one element from A and one from B so that the sum is as large as…
- Balloons span closed integer intervals [1,4], [2,3], [4,6], [5,8], [7,9]. One arrow shot at integer x bursts every balloon whose interval…
- How many binary strings of length 6 contain neither two consecutive 0s nor three consecutive 1s?