Data Structures & AlgorithmsHardGATE CS
Bars of unit width have heights [3, 0, 2, 0, 4, 0, 1, 3, 0, 2, 5, 1, 0, 3]. After rain, how many units of water are trapped between the bars?
- A. 26
- B. 24
- C. 30
- D. 23
Correct answer: A. 26
At each position the water level is min(max to the left, max to the right) minus its height: total trapped = 26. The sum of heights (24) is not the water.
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
- For the array [3, 1, 2, 4, 1, 5, 2, 6], what is the sum of the minimum element over all contiguous subarrays?
- How many non-empty contiguous subarrays of [5, 2, -3, 8, 6, -9, 4, 7, 1, 3, 12, -5, 6, 2] have a sum divisible by 7?
- The infix expression ab^c^d-(e+fg)^h/i+j (^ is right-associative with highest precedence, then and /, then + and -, all others…
- For the pattern P = "abaabaabaabab", the KMP failure (prefix) function pi[i] is the length of the longest proper prefix of P[0..i] that is…
- The naive algorithm searches for P = "aaaab" in T = "aaabaaabaaaab": for each shift s = 0, 1, ..., it compares P[0], P[1], ... with T[s]…
- A queue is built from two stacks: an enqueue pushes onto the input stack; a dequeue pops from the output stack, and if the output stack is…