Ctrl K

Ranges With Alternating Parity

Medium
You are given an integer array values and a list of queries ranges, where ranges[i] = [from, to]. For each query, decide whether parity alternates on the slice values[from..to] (inclusive): every two neighbouring numbers in it must have different parity. A one-number slice always qualifies. Return a list of booleans where the i-th entry answers the i-th query. There can be many queries, so rescanning each slice from scratch is too slow.
Example 1
values=[7, 2, 9, 4, 4, 1], ranges=[[0, 3], [2, 5], [4, 4]] → [True, False, True]

7, 2, 9, 4 alternate odd and even, so true. In 9, 4, 4, 1 the two even 4s are neighbours, so false. A one-element range always passes, so true.

Example 2
values=[5, 11, 6], ranges=[[0, 2], [1, 2]] → [False, True]

5 and 11 are both odd, so the first range fails; in the second, 11 and 6 differ in parity.

Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 1 ≤ values[i] ≤ 10⁵
  • 1 ≤ len(ranges) ≤ 10⁵
  • 0 ≤ from ≤ to ≤ len(values) − 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShortest Stretch to the Goal MediumWindows With a Small Product MediumLongest Run of Ones With k Fixes MediumLargest Light Square MediumTrim the Ends to a Target Medium
loading Python…
Input
values=[7, 2, 9, 4, 4, 1], ranges=[[0, 3], [2, 5], [4, 4]]
Expected
[True, False, True]

Run checks the first 3 tests in your browser. Submit checks all 82 tests on the server, hidden ones included.