Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT
How many non-empty contiguous subarrays of [1, -1, 2, 0, 1, 3, -2, 1] have a sum equal to their length (number of elements)? Transform each element to a[i] − 1 and count zero-sum subarrays.
- A. 8
- B. 7
- C. 5
- D. 3
Correct answer: B. 7
Subtract 1 from every element: the condition becomes a zero-sum subarray, which is counted with prefix sums and a frequency map. Checking each element against 1 misses the longer subarrays.
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
- Choose any subset of the positions of [4, -2, 7, 3, 9, 1, -5, 8, 2] (possibly empty) so that every two chosen positions are at least 3…
- Assertion (A): Dynamic programming over subsets for TSP needs exponential time in the number of cities. Reason (R): The number of subsets…
- Assertion (A): Kadane's algorithm can be used to find the maximum subarray sum in O(n) time. Reason (R): It keeps the best sum ending at…
- Assertion (A): Binary search on the answer requires that the feasibility predicate is monotone. Reason (R): Only a monotone predicate lets…
- Assertion (A): The greedy 'largest coin first' is optimal for the coin set {1, 3, 4} when the amount is 6. Reason (R): A coin system whose…
- Which of the following statements about the sliding window technique on arrays with NEGATIVE numbers is/are correct? I. A plain…