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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code