Data Structures & AlgorithmsHard
What is the length of the longest contiguous subarray of 2 0 1 3 1 1 2 0 in which the sum of (element − 1) over the subarray equals 0?
- A. 5
- B. 6
- C. 1
- D. 4
Correct answer: D. 4
Subtracting one from each element turns the condition into a zero-sum subarray, and the longest one comes from first-occurrence prefix sums. Comparing the sum to the length gives the same test only when the subtraction is applied to every element, which the question specifies.
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 positions i with 1 ≤ i ≤ n−2 in 5 3 6 2 7 4 8 1 9 are STRICT local minima (strictly smaller than both neighbours)?
- How many contiguous windows of length exactly 2 in 2 3 1 4 2 5 0 3 have a PRODUCT strictly greater than their SUM?
- How many contiguous windows of length exactly 3 in 6 10 15 4 9 7 14 3 have their three values PAIRWISE coprime (every two of them have gcd…
- How many subsequences of EXACTLY three elements of 6 10 15 4 9 (position subsets) have gcd equal to 1?
- How many contiguous windows of length exactly 2 in 4 7 2 9 3 5 11 8 have BOTH values prime?
- Why does the two-transaction stock problem split the array at a point and solve each side independently?