My approach was to loop through the nums arr and check the equality of the parities of adjacent elements, so it checks if elements at i and i+1 are either both even or both odd
class Solution {
public:
bool isArraySpecial(vector<int>& nums) {
for(int i = 0; i < nums.size() - 1; i++) {
if(nums.at(i) % 2 == nums.at(i+1) % 2) {
return false;
}
}
return true;
}
};
- Time: O(n)
- Space: O(1)