Data Structures & AlgorithmsHard
What is the length of the longest contiguous subarray of 4 6 3 8 10 12 5 2 4 in which EVERY element is even and whose length is odd?
- A. 3
- B. 5
- C. 9
- D. 4
Correct answer: A. 3
Find the maximal runs of even values, then take the largest odd length that fits inside one run. Ignoring the length parity gives the even-only maximum, and allowing one odd element changes the set.
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 1 3 2 4 5 3 1 6 have a sum divisible by 4 AND an odd length?
- How many index pairs i < j in 2 3 4 6 8 12 9 satisfy A[i]·A[j] = 24?
- A min-stack stores a pair (value, current minimum) per entry. Why does popping never require recomputing the minimum?
- In the 'remove nodes with a greater value to their right' problem, which list order is processed to decide survivors in O(n)?
- In the asteroid collision problem, why does a stack of right-moving asteroids suffice?
- A linear-time check decides whether a permutation can be sorted by one stack. Which property is equivalent to being stack-sortable?