Data Structures & AlgorithmsHardGATE CS · DSA
Heights [6, 3, 1, 5, 9, 8, 9, 6, 4] represent vertical lines at each index. What is the maximum water area (min(height) × horizontal distance) obtainable between any two lines?
- A. 40
- B. 42
- C. 8
- D. 32
Correct answer: B. 42
Checking all pairs (equivalently, the two-pointer sweep that moves the shorter boundary inward) finds the best area is 42, between indices 0 and 7. Only using the two outermost lines, 32, is not always optimal since a taller inner pair can win.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Kadane's algorithm runs on the array [-9, 1, 7, 5, 7, -3, 0, -7, 1] to find the maximum sum of a contiguous subarray. What sum does it…
- QuickSort (last element as pivot, Lomuto-style partition) sorts [57, 6, 37, 10, 15, 58]. Counting one comparison per element checked…
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [2, 0, 0, 1, 0, 1, 1, 0] (values 0, 1, 2 only) in…
- Array [87, 4, 31, 36, 38, 63, 75] is a sorted array rotated at pivot 6. Using the standard O(log n) rotated-array binary search (decide…
- Array [56, 73, 74, 82, 90, 3, 48] is a sorted array rotated at pivot 2. Using the standard O(log n) rotated-array binary search (decide…