← leetcode

Special Array I

· Easy

LeetCode problem 3151

Simply, iterate through the array and check that the parity of the previous element is different from the parity of the current element

code link to code

class Solution {
public:
    bool isArraySpecial(vector<int>& nums) {
        int parity = nums[0] % 2;
        for (int i = 1; i < nums.size(); ++i) {
            if (parity != nums[i] % 2) {
                parity ^= 1;
            } else {
                return false;
            }
        }
        return true;
    }
};

complexity link to complexity

  • Time: O(n)
  • Space: O(1)