Data Structures & AlgorithmsVery hardCodeforces Div 2 C · ICPC regional
Heights h = [1, 8, 6, 2, 5, 4, 8, 3, 7, 9] are drawn as vertical lines at positions 0 to 9. What is the largest amount of water that two of these lines can hold together with the x-axis?
- A. 9
- B. 58
- C. 81
- D. 64
Correct answer: D. 64
Area is width times the shorter height. Always moving the shorter side inward is safe because the taller side cannot give a larger area at the same width. The best pair here gives 64; moving the taller pointer instead can skip it.
More Coding Interview Problems questions
- Sorted positive array a = [1, 2, 3, 4, 6, 8]. How many pairs of indices i < j have a[i] a[j] <= 12?
- 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…