Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: Count the subarrays of [4, 2, 2, 4, 2] whose maximum equals the minimum (single elements count).
- A. 1
- B. 15
- C. 3
- D. 6
Correct answer: D. 6
max = min means all elements are equal: the 5 single elements plus the run [2, 2], i.e. 6 (sum k(k + 1)/2 over runs of equal values). Leaving out single elements gives 1, 15 counts every subarray, and counting distinct contents {4}, {2}, {2, 2} gives 3.
More Coding Interview Problems questions
- Problem: Water is poured into the cells of a 1-dimensional array of heights [0,1,0,2,1,0,1,3,2,1,2,1]; each cell holds water up to…
- Problem: Starting at 0 on a line, a walker takes steps +3, −2, +3, −2, … alternating, with 12 steps in total. How many distinct integer…
- Problem: Count the sequences of length 5 over {0, 1, 2} with no two adjacent equal digits and whose first digit is 0.
- Problem: The sorted array [1, 4, 4, 9, 12, 12, 15, 20] (0-indexed) may contain repeated values. Using a lower-bound search, which returns…
- Problem: How many subarrays of [1, 2, 2, 3] have max minus min at most 1? Run the code. python a = [1, 2, 2, 3] print(sum(1 for i in…
- Problem: How many peaks (elements strictly greater than both neighbours, excluding the two ends) does [1, 3, 2, 4, 1, 5] have? Run the…