Skip to content

← 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

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

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