Data Structures & AlgorithmsHardCodeforces Div 2 C · ICPC regional
Sorted positive array a = [1, 2, 3, 4, 6, 8]. How many pairs of indices i < j have a[i] * a[j] <= 12?
- A. 9
- B. 7
- C. 12
- D. 18
Correct answer: A. 9
With sorted positive values the valid partners of each i form a shrinking prefix, so two pointers count 5 + 3 + 1 = 9 pairs (1 with everything, 2 with 3, 4, 6, and 3 with 4). Using < 12 drops (2, 6) and (3, 4) (7), letting j start at i adds 1·1, 2·2, 3·3 (12), and counting ordered pairs gives 18.
More Coding Interview Problems questions
- Sorted array a = [1, 1, 2, 2, 2, 3, 4, 4]. How many index pairs (i < j) satisfy a[i] + a[j] = 5? Count positions, not distinct value pairs.
- Sorted array a = [-2, -1, -1, 0, 1, 1, 2, 2]. How many DISTINCT value triples (x, y, z) with x <= y <= z taken from the array's values sum…
- Two sorted arrays A = [1, 4, 7, 9] and B = [2, 3, 8, 10, 12] are merged in sorted order with two pointers. What is the 6th smallest…
- Sorted array a = [-3, -1, 0, 2, 4, 5, 9]. How many index pairs i < j have -1 <= a[i] + a[j] <= 5?
- Four towns have road distances M = [[0,10,15,20],[10,0,35,25],[15,35,0,30],[20,25,30,0]] (symmetric). A salesperson starts at town 0…
- The 4-cycle graph 0-1-2-3-0 (undirected, so both directions of every edge exist) is given as an adjacency matrix. How many ordered…