Data Structures & AlgorithmsModerateTCS NQT · Infosys
How many contiguous subarrays of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sum to exactly 15?
- A. 4
- B. 3
- C. 2
- D. 0
Correct answer: B. 3
Count pairs of prefix sums that differ by 15 with a hash map of prefix values. The consecutive runs 7+8, 4+5+6, 1+2+3+4+5 and others each give one subarray; the total is the number of such runs.
More Coding Interview Problems questions
- Array a = [9, 4, 7, 1, 8, 2, 6, 3]. How many index pairs i < j have |a[i] - a[j]| >= 5?
- Among [5, 3, 6, 12, 9, 10, 1], choose two different elements. What is the largest possible value of x OR y?
- How many pairs of distinct elements of [1, 4, 6, 7, 10, 13, 15] have a sum that is a perfect square (1, 4, 9, 16, ...)?
- Closed intervals [2,9], [4,7], [6,12], [10,14], [11,13], [15,18]. What is the total length of the set of points covered by at least one…
- With coins of values 1, 3 and 4 (each usable any number of times), in how many ways (order does not matter) can you make the amount 6?
- What is the length of the longest run of equal consecutive values in [4, 4, 1, 2, 2, 2, 5, 1]?