Data Structures & AlgorithmsModerateTCS NQT · Infosys
Array a = [1, 2, 3, 4, -1, 5, 0, 2]. How many contiguous subarrays have a sum of exactly 10?
- A. 2
- B. 3
- C. 1
- D. 4
Correct answer: A. 2
Prefix sums give every subarray sum as a difference of two prefixes, so counting pairs whose difference is 10 with a hash map counts all subarrays. Zero-valued elements create several subarrays with the same sum, which the count must keep.
More Coding Interview Problems questions
- Array a = [2, -2, 3, -3, 4, -4, 1]. How many DISTINCT values appear among the prefix sums P[0], P[1], ..., P[7] (including P[0] = 0)?
- Sorted arrays A = [3, 8, 12, 17, 25] and B = [5, 9, 14, 20]. Choose one element from A and one from B so that the sum is as large as…
- Balloons span closed integer intervals [1,4], [2,3], [4,6], [5,8], [7,9]. One arrow shot at integer x bursts every balloon whose interval…
- How many binary strings of length 6 contain neither two consecutive 0s nor three consecutive 1s?
- How many subsets (including the empty subset) of [3, 5, 9, 11, 14, 2] have a sum divisible by 7?
- Closed intervals [1,4], [2,6], [4,7], [5,9], [8,10] are given. What is the largest number of intervals that share a single integer point?