Problem Description
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
.
Key Insights
- A single element array is always special.
- For an array with more than one element, check adjacent pairs to ensure one is even and the other is odd.
- The parity of a number can be determined using the modulus operator:
num % 2
.
Space and Time Complexity
Time Complexity: O(n) - where n is the number of elements in the array, since we need to check adjacent pairs. Space Complexity: O(1) - we are using a constant amount of space for variables regardless of the input size.
Solution
To determine if the array is special, we will iterate through the array and check each adjacent pair of elements. We will use a loop to go through the array and compare the parity of each element with the next. If we find any adjacent elements that have the same parity (both even or both odd), we return false
. Otherwise, after checking all pairs, we return true
.