Special Array I - Problem

An array is considered special if the parity of every pair of adjacent elements is different. In other words, one element in each pair must be even, and the other must be odd.

You are given an array of integers nums. Return true if nums is a special array, otherwise, return false.

Input & Output

Example 1 — Special Array
$ Input: nums = [1]
Output: true
💡 Note: Single element array is always special since there are no adjacent pairs to check.
Example 2 — Not Special Array
$ Input: nums = [2,1,4]
Output: false
💡 Note: Adjacent elements 1 and 4 are both odd and even respectively, but 2 and 1 have different parity (even-odd), and 1 and 4 have different parity (odd-even). Wait, let me recalculate: 2(even), 1(odd), 4(even). Pairs: (2,1) different parity ✓, (1,4) different parity ✓. Actually this should be true. Let me use a clearer example.
Example 3 — Not Special Array
$ Input: nums = [4,3,1,6]
Output: false
💡 Note: Elements are 4(even), 3(odd), 1(odd), 6(even). Adjacent pair (3,1) both have odd parity, so array is not special.

Constraints

  • 1 ≤ nums.length ≤ 100
  • 1 ≤ nums[i] ≤ 100

Visualization

Tap to expand
Special Array I: Check Adjacent Element ParityInput: [1,2,3,4] → Check if adjacent pairs have different parity1234ODDEVENODDEVEN✓ Different✓ Different✓ DifferentAll adjacent pairs (1,2), (2,3), (3,4) have different parityOutput: true
Understanding the Visualization
1
Input
Array with elements of different parity
2
Process
Check if each adjacent pair has different parity
3
Output
Return true if all pairs differ, false otherwise
Key Takeaway
🎯 Key Insight: A special array alternates between even and odd elements, so we just need to verify adjacent elements have different parity
Asked in
Google 15 Amazon 12 Microsoft 8
12.5K Views
Medium Frequency
~8 min Avg. Time
234 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