Data Structures & AlgorithmsVery hardTCS NQT · Accenture
How many distinct values appear among the sums of all non-empty contiguous subarrays of [1, 2, 3]?
- A. 5
- B. 6
- C. 4
- D. 7
Correct answer: A. 5
List the six subarray sums: 1, 2, 3, 3, 5, 6. Counting subarrays gives 6, but the question asks for distinct sums.
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
- How many subarrays of [2, 5, 1, 5, 3] have a maximum value that occurs exactly once inside the subarray?
- How many elements of the array [3, 1, 4, 1, 5, 9, 2, 6] have no strictly greater element anywhere to their right?
- How many index pairs i < j in the array [6, 9, 2, 7, 4, 1] satisfy a[i] + i = a[j] + j (0-based indices)?
- For the array [3, -1, -5, 4, 2, -6, 7], how many non-empty contiguous subarrays have a sum divisible by 4? Remainders are taken as the…
- How many contiguous subarrays of [5, 2, 7, 1, 4, 6] have bitwise XOR exactly 3? Use prefix XOR values p[0]=0, p[i]=B[0]^…^B[i−1].
- In the array [1, 2, 1, 3, 2, 1, 4, 5], how many contiguous subarrays contain exactly 2 odd numbers?