LeetCode Entry

1550. Three Consecutive Odds

11.05.2025 easy 2025 kotlin rust

odds

1550. Three Consecutive Odds easy blog post substack youtube 1.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/985

Problem TLDR

3 odds #easy #bitmask

Intuition

Count odds.

Approach

  • use bit & 1 to check for odds
  • use bitmask 0b111 = 7 to check for 3 odds

Complexity

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

  • Space complexity: \(O(1)\)

Code

```kotlin []

// 22ms fun threeConsecutiveOdds(a: IntArray) = “1, 1, 1” in “” + a.map { it % 2 }

```kotlin

// 21ms
    fun threeConsecutiveOdds(a: IntArray) =
        a.asList().windowed(3).any { it.all { it % 2 > 0 }}


// 23ms
    fun threeConsecutiveOdds(a: IntArray) =
        a.asList().windowed(3).any { it.reduce(Int::and) % 2 > 0 }


// 4ms
    fun threeConsecutiveOdds(a: IntArray) = (1..<a.size - 1)
        .any { a[it - 1] and a[it] and a[it + 1] % 2 > 0 }


// 0ms https://leetcode.com/problems/three-consecutive-odds/submissions/1630809266
    fun threeConsecutiveOdds(a: IntArray): Boolean {
        var c = 0
        return a.any { c = (it % 2) * (c + 1); c > 2 }
    }


// 0ms
    fun threeConsecutiveOdds(a: IntArray): Boolean {
        var c = 0
        return a.any { c = it and 1 or (c shl 1) and 7; c > 6 }
    }


// 0ms https://leetcode.com/problems/three-consecutive-odds/submissions/1630796680
    pub fn three_consecutive_odds(a: Vec<i32>) -> bool {
        a[..].windows(3).any(|w| 0 < 1 & w[0] & w[1] & w[2])
    }


// 0ms
    bool threeConsecutiveOdds(vector<int>& a) {
        for(int c = 0; int &x: a) if ((c = x & 1 | (c << 1) & 7) > 6)
        return 1; return 0;
    }