↓Skip to main content
  1. LeetCode/

LeetCode 3688: Bitwise OR of Even Numbers in an Array

·1 min· ·
LeetCode Easy Bit-Manipulation Array Simulation
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2025-09-21

  • Submissions: Accepted (first try)

Approach
#

Even numbers have lowest bit 0; OR them all together (Bit Manipulation Template 1). OR starts from 0, which stays untouched when there are no even numbers, exactly the required 0.

My Solution
#

class Solution:
    def evenNumberBitwiseORs(self, nums: List[int]) -> int:
        res = 0
        for x in nums:
            if x % 2 == 0:
                res |= x
        return res

I originally filtered first and special-cased an empty list; starting from 0 removes the special case.

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • The identity for OR is 0 and for AND is -1; do not mix them up.

Interview Explanation
#

I OR only the even numbers starting from 0; with none, the result is naturally 0. Time O(n).

Related

LeetCode 1920: Build Array from Permutation
·1 min
LeetCode Easy Array Simulation
LeetCode 1920 note: follow the definition ans[i] = nums[nums[i]]; for O(1) extra space, stack the new value times n on top of the old one
LeetCode 3507: Minimum Pair Removal to Sort Array I
·2 mins
LeetCode Easy Simulation Array
LeetCode 3507 note: n is tiny, so simulate; the right side of a slice assignment must be a list
LeetCode 3069: Distribute Elements Into Two Arrays I
·1 min
LeetCode Easy Array Two-Pointers Simulation
LeetCode 3069 note: simulate the rule: the first two go to separate arrays, then each element joins the array whose last element is larger
LeetCode 2859: Sum of Values at Indices With K Set Bits
·1 min
LeetCode Easy Array Bit-Manipulation
LeetCode 2859 note: count the set bits of the index, not of the value
LeetCode 682: Baseball Game
·1 min
LeetCode Easy Array Stack Simulation
LeetCode 682 note: keep valid scores on a stack; C removes the last, D adds its double, and + adds the sum of the last two
LeetCode 136: Single Number
·1 min
LeetCode Easy Array Bit-Manipulation
LeetCode 136 note: x ^ x = 0 and x ^ 0 = x, so XOR everything; pairs cancel and the single number remains