Skip to main content
Back to problems
Leetcode
Medium
Arrays
Special Array II

Answer many range queries about whether a subarray is special, using a precomputed parity boundary summary.

Acceptance 0%
Problem Statement

Problem

You are given an integer array nums and multiple queries. For each query [l, r], determine whether the subarray nums[l...r] is special.

A subarray is special if every pair of adjacent elements in it alternates in parity: one is even, the next is odd, then even, and so on, for the entire subarray.

In other words, for every index i with l <= i < r, nums[i] and nums[i + 1] must have different parity.

Return the answer for every query.

Notes

  • A single-element subarray is always special.
  • Indices are zero-based.
  • You should handle many queries efficiently.

Input Format

Input

  • An integer array nums.
  • A list of queries, where each query is a pair [l, r] representing a subarray range.

Output Format

Output

  • Return a list of booleans, where the i-th value indicates whether nums[l...r] is special for query i.

Constraints

Constraints

  • 1 <= nums.length
  • 0 <= l <= r < nums.length
  • The number of queries may be large, so per-query linear scanning is not ideal.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [3, 4, 1, 2, 7], queries = [[0, 4], [1, 3], [2, 2]]

Output

[true, true, true]

Explanation

The full array alternates parity at every step. The subarray [4,1,2] also alternates. A one-element subarray is always special.

Example 2

Input

nums = [2, 4, 6, 1], queries = [[0, 2], [1, 3]]

Output

[false, false]

Explanation

[2,4,6] has adjacent pairs with the same parity, so it is not special. [4,6,1] also contains the same-parity pair [4,6].

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.