Zigzag Parity Check on Subarray Ranges
Arrays & HashingAuto-graded · 22 test casesLast updated
Answer many range queries asking whether neighbors in a subarray always differ in odd/even parity, using prefix counts of same-parity adjacent pairs.
Zigzag Parity Check on Subarray Ranges
You are given a list of integers nums and a list of range queries. A contiguous segment of nums is called a zigzag segment if every two neighboring elements inside it have opposite parity: one is even and the other is odd.
Each query is a pair [left, right] (both indices inclusive). For every query, decide whether the segment nums[left..right] is a zigzag segment.
Return a list of booleans, one per query and in the same order as the queries. An entry is True if the corresponding segment is a zigzag segment and False otherwise.
Rules
- A segment with a single element (
left == right) has no neighboring pairs, so it is always a zigzag segment. - Zero counts as even. Negative numbers follow the usual meaning of parity (for example,
-3is odd and-4is even). - Only pairs of neighbors that lie fully inside
[left, right]matter. The element just beforeleftand the element just afterrightare ignored.
Constraints
1 <= len(nums) <= 10^51 <= len(queries) <= 10^50 <= left <= right < len(nums)- Each query is a pair of integers
[left, right]. - Integers in
numsmay be negative, zero or positive.
Examples
Example 1
Input: zigzag_parity_queries([1, 2, 3, 4, 5], [[0, 2], [0, 4], [1, 4]])
Output: [True, True, True]
Example 2
Input: zigzag_parity_queries([1, 3, 5], [[0, 1], [1, 2]])
Output: [False, False]
Hints
Hint 1
Rephrase the condition: a segment fails only if it contains at least one pair of neighbors with the same parity. So count such violations instead of checking parities one by one.
Hint 2
Define a marker for each position i >= 1 that is 1 when nums[i-1] and nums[i] share parity. Build prefix sums over these markers so any range of markers can be summed in O(1).
Hint 3
For the query [left, right], the neighbor pairs inside it are those ending at left+1 through right. The pair ending at left involves nums[left-1], which is outside the segment, so it must not be counted.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
A segment is a zigzag segment exactly when none of the neighbor pairs inside it has matching parity. So the task becomes: count "same-parity" neighbor pairs inside a range and check that the count is zero.
Precompute an array same where same[i] is the number of indices j in [1, i] such that nums[j-1] and nums[j] have the same parity (with same[0] = 0). This is a running prefix sum over a 0/1 marker for each adjacent pair.
For a query [left, right], the neighbor pairs fully inside the segment are the pairs ending at positions left+1, ..., right. Their total number of violations is same[right] - same[left]. The pair ending at left is excluded because it involves nums[left-1], which lies outside the segment. The answer is True when this difference is zero.
Why it is correct
- Every term in the prefix sum is non-negative, so the difference is zero if and only if every marker in
left+1..rightis zero, i.e. every inner neighbor pair has opposite parity. - When
left == right, the range of pairs is empty and the difference is zero, so single elements correctly giveTrue. - In Python,
x % 2is always0or1, even for negativex, so negatives and zero are handled correctly. In languages where%can be negative, usex & 1instead.
Complexity
- Preprocessing: O(n) time, O(n) space.
- Each query: O(1) time, so O(q) for all queries.
- Total: O(n + q) time, versus O(n * q) for scanning every range directly.
Reference solution
from typing import List, Tuple
def zigzag_parity_queries(nums: List[int], queries: List[Tuple[int, int]]) -> List[bool]:
n = len(nums)
# same[i] = number of positions j in [1, i] where nums[j-1] and nums[j] share parity
same = [0] * n
for i in range(1, n):
same[i] = same[i - 1] + (1 if nums[i] % 2 == nums[i - 1] % 2 else 0)
result = []
for left, right in queries:
result.append(same[right] == same[left])
return result