leetcode

Bitwise OR of Even Numbers in an Array - Link

Question Description

You are given an integer array nums. Return the bitwise OR of all even numbers in the array.

If there are no even numbers in nums, return 0.


Constraints


Approach

Iterate through the array and perform bitwise OR operation on all even numbers found.

Algorithm:

  1. Initialize result = 0
  2. For each number in the array:
    • If the number is even (num % 2 == 0), perform bitwise OR with result
    • If the number is odd, skip it
  3. Return the final result

This approach works because:


Dry Run

Example 1: nums = [1, 2, 3, 4, 5, 6]

Step-by-step execution:

Result: 6

Example 2: nums = [1, 3, 5]

Step-by-step execution:

Result: 0 (no even numbers)

Example 3: nums = [2, 4, 6, 8]

Step-by-step execution:

Result: 14


Solution

class Solution {
    public int evenNumberBitwiseORs(int[] nums) {
        int res = 0;
        for (int num : nums) {
            if (num % 2 == 0) {
                res |= num;
            }
        }
        return res;
    }
}

Time and Space Complexity


Alternative Approaches

Early Termination Approach

class Solution {
    public int evenNumberBitwiseORs(int[] nums) {
        int result = 0;
        for (int num : nums) {
            if (num % 2 == 0) {
                result |= num;
            }
        }
        return result;
    }
}

Explanation: Same as main approach but with more descriptive variable name.

Time Complexity: O(n) - single pass Space Complexity: O(1) - constant space

Stream API Approach (Java 8+)

class Solution {
    public int evenNumberBitwiseORs(int[] nums) {
        return Arrays.stream(nums)
                    .filter(num -> num % 2 == 0)
                    .reduce(0, (a, b) -> a | b);
    }
}

Explanation: Use Java streams to filter even numbers and reduce with OR operation.

Time Complexity: O(n) - stream processing Space Complexity: O(1) - excluding temporary stream objects

Bitwise Check Approach

class Solution {
    public int evenNumberBitwiseORs(int[] nums) {
        int result = 0;
        for (int num : nums) {
            if ((num & 1) == 0) {  // Check if even using bitwise AND
                result |= num;
            }
        }
        return result;
    }
}

Explanation: Use bitwise AND with 1 to check if number is even (even numbers have LSB 0).

Time Complexity: O(n) - single pass Space Complexity: O(1) - constant space

Index-Based Approach

class Solution {
    public int evenNumberBitwiseORs(int[] nums) {
        int result = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] % 2 == 0) {
                result = result | nums[i];
            }
        }
        return result;
    }
}

Explanation: Use traditional for loop with index instead of enhanced for loop.

Time Complexity: O(n) - single pass Space Complexity: O(1) - constant space


Bitwise OR Properties

Property Description Example
Identity x | 0 = x 5 | 0 = 5
Self x | x = x 5 | 5 = 5
Commutative x | y = y | x 3 | 5 = 5 | 3
Associative (x | y) | z = x | (y | z) (1 | 2) | 4 = 1 | (2 | 4)

Key Insights:

Binary Examples:

Edge Cases: