3151. Special Array I
Description
An array is considered special if every pair of its adjacent elements contains two numbers with different parity.
You are given an array of integers nums. Return true if nums is a special array, otherwise, return false.
Example 1:
1 2 3 4 5 6 7
| Input: nums = [1]
Output: true
Explanation:
There is only one element. So the answer is `true`.
|
Example 2:
1 2 3 4 5 6 7
| Input: nums = [2,1,4]
Output: true
Explanation:
There is only two pairs: `(2,1)` and `(1,4)`, and both of them contain numbers with different parity. So the answer is `true`.
|
Example 3:
1 2 3 4 5 6 7
| Input: nums = [4,3,1,6]
Output: false
Explanation:
`nums[1]` and `nums[2]` are both odd. So the answer is `false`.
|
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100
Hints/Notes
Solution
Language: C++
1 2 3 4 5 6 7 8 9 10 11 12 13
| class Solution { public: bool isArraySpecial(vector<int>& nums) { bool isSpecial = true; for (int i = 0; i < nums.size() - 1; i++) { if (!((nums[i] % 2) ^ (nums[i + 1] % 2))) { isSpecial = false; break; } } return isSpecial; } };
|