Data Structures & AlgorithmsHardGATE CS · TCS NQT
How many contiguous subarrays of [1, -1, 2, -2, 3, -3, 1] sum to exactly 1?
- A. 5
- B. 7
- C. 2
- D. 3
Correct answer: B. 7
Prefix sums with a hashmap count pairs whose difference is 1, with prefix 0 seeded: 7. Forgetting the seed drops the subarrays that start at index 0 (5); a sliding window, invalid with negatives, finds 3, and 2 counts only the single elements equal to 1.
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 contiguous subarrays of [2, 5, 1, 4, 3] have their maximum equal to the first or the last element of the subarray?
- How many disjoint intervals remain after merging all overlapping closed intervals in [(1,3),(2,6),(8,10),(15,18),(17,20),(6,7)]?…
- How many index triples i < j < k in [2, 3, 4, 5, 7, 10] can be the three sides of a non-degenerate triangle (sum of the two smaller sides…
- How many index pairs i < j in [3, 5, 6, 8, 1] satisfy a[i] + a[j] > a[i] XOR a[j]?
- What is the smallest positive integer divisor d such that the sum of ceil(x / d) over [1, 2, 5, 9] is at most 6?
- Sort [6, 3, 4, 1, 2, 5] so that all even numbers come first in increasing order, then all odd numbers in increasing order, using adjacent…