My first instinct was to just iterate through each query range and check adjacent pairs, which is obviously too slow when there are a lot of queries.
Preprocess the array by creating a boolean array where each index i (from 1 to n-1) indicates whether arr[i-1] and arr[i] have strictly alternating parity. Then build a prefix sum array over these booleans. For a query [l, r], check if the sum of alternating indicators from l+1 to r equals r - l, which means all adjacent pairs in the subarray alternate.
Pro tip: Clarify the indexing convention (0-based or 1-based) and handle edge cases like l == r (single element) and empty subarrays. Also, mention that the preprocessing takes O(n) time and O(n) space, and each query is O(1).
Restate the problem: For each query [l, r], determine if every adjacent pair in the subarray has different parity. Confirm that 'strictly alternating parity' means arr[i] % 2 != arr[i+1] % 2 for all i in [l, r-1].
Create a boolean array alt of length n-1, where alt[i] = true if arr[i] and arr[i+1] have different parity, for i from 0 to n-2.
Construct a prefix sum array pref where pref[0] = 0 and pref[i+1] = pref[i] + (alt[i] ? 1 : 0). This allows O(1) range sum queries.
For a query [l, r] (0-based, inclusive), if l == r, return true. Otherwise, compute the number of alternating pairs in the subarray as pref[r] - pref[l]. The subarray is valid if and only if this count equals r - l.
Preprocessing takes O(n) time and O(n) space. Each query is O(1). Handle edge cases: single-element subarray (always true), and ensure indices are within bounds.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.