LeetCode solutions

3427. Special Array II

My accepted Python solution to LeetCode problem 3427, Special Array II, running in 35ms.

  • Difficulty: Medium
  • Python
  • Runtime 35ms
  • Memory 45.9MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 35ms, memory 45.9MB, accepted 2025-12-31.

python
class Solution:
    def isArraySpecial(self, nums: List[int], queries: List[List[int]]) -> List[bool]:
        n = len(nums)
        
        # prefix[i] = number of "bad" pairs (same parity) in nums[0:i+1]
        prefix = [0] * n
        for i in range(1, n):
            prefix[i] = prefix[i-1] + (1 if nums[i] % 2 == nums[i-1] % 2 else 0)
        
        result = []
        for l, r in queries:
            # Check if there are any bad pairs in range [l, r]
            bad_pairs = prefix[r] - prefix[l]
            result.append(bad_pairs == 0)
        
        return result

Source