Special Array I - Problem

An array is considered special if every pair of adjacent elements has different parity - meaning one element must be even and the other must be odd.

Given an array of integers nums, your task is to determine whether it forms a special array. Return true if the array is special, otherwise return false.

What makes an array special?

  • Each adjacent pair (nums[i], nums[i+1]) must have different parity
  • If one element is even, its neighbor must be odd
  • The pattern can be either even-odd-even-odd... or odd-even-odd-even...

Example: [1, 2, 3, 4] is special because: 1(odd)-2(even)-3(odd)-4(even)

Input & Output

example_1.py โ€” Basic Special Array
$ Input: [1]
โ€บ Output: true
๐Ÿ’ก Note: Single element arrays are always special by definition since there are no adjacent pairs to violate the rule.
example_2.py โ€” Alternating Pattern
$ Input: [2, 1, 4]
โ€บ Output: true
๐Ÿ’ก Note: Array follows even-odd-even pattern: 2(even) โ‰  1(odd) โ‰  4(even). All adjacent pairs have different parity.
example_3.py โ€” Same Parity Violation
$ Input: [4, 3, 1, 6]
โ€บ Output: false
๐Ÿ’ก Note: Elements 3 and 1 are both odd, violating the special array rule. Adjacent elements must have different parity.

Constraints

  • 1 โ‰ค nums.length โ‰ค 100
  • 1 โ‰ค nums[i] โ‰ค 100
  • Note: Arrays with 0 or 1 elements are considered special

Visualization

Tap to expand
Special Array: Checkerboard Patternโœ“ SPECIAL: [1, 2, 3, 4]1234oddevenoddevenโœ— NOT SPECIAL: [2, 4, 6]246evenevenevenSame parity!Algorithm Steps1Check array length โ‰ค 12Iterate through adjacent pairs3Check if nums[i] % 2 == nums[i+1] % 24Return false if same parity found5Return true if all pairs validTime: O(n) | Space: O(1)
Understanding the Visualization
1
Identify Pattern
Special arrays alternate between odd and even numbers
2
Single Pass Check
Walk through array once, checking each adjacent pair
3
Early Termination
Return false immediately when same parity is found
Key Takeaway
๐ŸŽฏ Key Insight: A special array is like a perfect checkerboard - adjacent elements must always have different parity (odd/even). One linear pass is sufficient to validate this property.
Asked in
Meta 25 Google 18 Amazon 15 Microsoft 12
35.0K Views
Medium Frequency
~8 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen