Data Structures & AlgorithmsHard
How many contiguous subarrays of 3 1 2 4 2 3 1 2 have a sum EXACTLY equal to twice their length?
- A. 9
- B. 2
- C. 32
- D. 7
Correct answer: A. 9
Subtract 2 from every element and count zero-sum subarrays, which is a prefix-sum count. Equating the sum to the length, or using ≥ instead of =, answers a different question.
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
- What is the maximum sum of a contiguous subarray of 4 2 4 5 6 2 8 that contains NO repeated value?
- How many contiguous subarrays of length at least 2 of 1 1 2 2 2 3 1 1 1 1 consist of ONE repeated value?
- Treat 5 -2 3 4 -6 1 as a CIRCULAR array. What is the largest sum of three consecutive (circular) elements?
- How many index pairs i < j in 1 4 1 4 4 2 1 2 2 hold EQUAL values and satisfy j − i ≤ 2?
- How many index triples i < k < j in 2 4 6 3 9 5 satisfy A[i] + A[j] = 2·A[k] (the middle value is the arithmetic mean of the outer two)?
- What is the length of the longest contiguous subarray of 1 2 3 5 1 2 3 such that the sum of EVERY prefix of that subarray is divisible by 3?