Data Structures & AlgorithmsHardGATE CS · TCS NQT
Which technique detects whether an array contains a non-empty subarray with sum zero in O(n) expected time?
- A. Sorting the array and binary search for zero
- B. Two pointers from both ends
- C. Dynamic programming over all subarrays
- D. Prefix sums stored in a hash set (repeat means a zero-sum subarray)
Correct answer: D. Prefix sums stored in a hash set (repeat means a zero-sum subarray)
If two prefix sums are equal, the elements between them sum to zero. Checking prefix sums against a hash set finds any such repeat in one pass.
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
- An array of 200000 integers may contain negative values, and you must count its contiguous subarrays whose sum equals K in near-linear…
- For a string of length 10^6 and a pattern of 20 letters, you need the shortest substring containing every pattern letter with…
- Given an array of 10^6 values and window size k, you need the maximum over all windows of the window minimum. What is the most suitable…
- How many contiguous subarrays of [1, 2, 1, 3, 2, 2, 1] contain at most two distinct values?
- You may increase any element of [2, 5, 3, 9] by 1 per operation, and you want all elements equal. What is the minimum number of operations?
- In the binary row [1, 1, 0, 1, 1, 1] treated as a histogram of height 1, what is the largest rectangle area?