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).
