Data Structures & AlgorithmsVery hard
How many contiguous subarrays of 2 1 4 3 6 5 8 7 of length at least 2 contain EQUAL numbers of even and odd values?
- A. 16
- B. 6
- C. 17
- D. 15
Correct answer: A. 16
Give each even +1 and each odd −1; equal counts is then a zero-sum subarray, found by prefix-sum hashing. Including singletons (which can never balance), checking the length parity, or testing a sum of 1 gives different totals.
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 length of the longest contiguous subarray of 3 6 9 4 12 18 5 in which EVERY element is divisible by the FIRST element of that…
- What is the maximum sum of a contiguous subarray of 3 1 3 2 4 1 5 whose elements are all DISTINCT?
- What is the MINIMUM sum of a contiguous subarray of 4 -2 1 -5 3 2 -1 whose length is at least 3?
- In the string "cbaebabacd", how many substrings of length 3 are ANAGRAMS of "abc" (same letters, any order)?
- What is the minimum number of CONSECUTIVE elements you must delete from 1 2 6 3 4 1 5 so that the remaining elements are in non-decreasing…
- How many contiguous windows of length 3 in 1 2 3 1 2 2 4 5 consist of three DIFFERENT values?