LeetCode Entry

3151. Special Array I

01.02.2025 easy 2025 kotlin rust

All siblings even-odd

3151. Special Array I easy blog post substack youtube 1.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/882

Problem TLDR

All siblings even-odd #easy

Intuition

Let’s golf

Approach

  • there is also a bitmask solution for i128 ints: only two masks possible 010101... and 101010...
  • can you make it shorter?

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(1)\) O(n) for Kotlin golf

Code


    fun isArraySpecial(nums: IntArray) =
        Regex("0, 0|1, 1") !in "${nums.map { it % 2 }}"


    pub fn is_array_special(nums: Vec<i32>) -> bool {
        (1..nums.len()).all(|i| nums[i] % 2 != nums[i - 1] % 2)
    }


    bool isArraySpecial(vector<int>& n) {
        int r = 1; for(int i = size(n); --i; r &= n[i] % 2 ^ n[i - 1] % 2); return r;
    }