Data Structures & AlgorithmsModerate
What is the length of the longest contiguous subarray of 2 3 4 5 7 8 9 10 1 that contains at most 2 odd numbers?
- A. 3
- B. 4
- C. 6
- D. 5
Correct answer: B. 4
Sliding window with a count of odd elements: extend right, and shrink left whenever the odd count exceeds 2. Limiting to at most one or to exactly two odd numbers, or allowing three, changes the answer.
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 pairs of positions i < j in 2 3 4 6 8 9 12 satisfy that A[j] is divisible by A[i]?
- How many contiguous windows of length 3 in 1 3 5 6 8 10 11 12 form an arithmetic progression (the two consecutive differences are equal)?
- How many index triples i < j < k in 3 1 4 2 5 6 have A[i] < A[j] < A[k] (a strictly increasing subsequence of length 3)?
- What is the length of the longest CONTIGUOUS subarray of 1 3 5 6 8 10 11 12 that is an arithmetic progression (all consecutive differences…
- How many contiguous subarrays of 1 4 2 6 3 5 0 have their FIRST element equal to their minimum AND their LAST element equal to their…
- How many contiguous subarrays of 3 1 6 2 9 4 5 12 contain EXACTLY 2 multiples of 3?