Data Structures & AlgorithmsHardGATE CS
The span of a day is the number of consecutive days ending that day (including it) whose price is at most that day's price. For prices [100, 80, 60, 70, 60, 75, 85, 90, 50, 95], what is the total of all the spans?
- A. 36
- B. 31
- C. 40
- D. 33
Correct answer: D. 33
The spans are [1, 1, 1, 2, 1, 4, 6, 7, 1, 9], which add to 33. Using a strict inequality (< instead of <=) changes the spans for repeated prices such as the two 60s.
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
- 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…
- 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]…