Data Structures & AlgorithmsHard
What is the length of the longest contiguous subarray of 1 6 2 7 3 5 4 8 1 9 that contains equally many values ≥ 5 and values < 5?
- A. 8
- B. 25
- C. 4
- D. 10
Correct answer: D. 10
Map ≥ 5 to +1 and < 5 to −1; the whole array has five of each, so its sum is 0 and the answer is 10. Stopping at the balanced prefix of length 8 misses the last two, using > 5 instead of ≥ 5 gives 4, and 25 counts the balanced subarrays instead of measuring the longest.
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 3 5 7 2 9 1 13 have a product that leaves remainder 1 when divided by 4?
- How many contiguous subarrays of 4 1 3 2 6 1 have a sum STRICTLY greater than the sum of the elements outside them?
- How many NON-EMPTY prefixes of 3 2 5 1 4 5 2 (the first k elements, k ≥ 1) have a sum divisible by 5?
- How many contiguous subarrays of length exactly 4 in 9 7 8 5 4 2 3 1 are STRICTLY decreasing?
- How many contiguous subarrays of 2 3 5 4 1 7 6 have a PRIME sum and an EVEN length?
- How many contiguous subarrays of length exactly 2 in 2 6 3 5 8 1 4 7 have a sum that is a multiple of 4 OR a product that is a multiple of…