Skip to main content

Bitwise OR of Even Numbers in an Array - Solution & Explanation

EasyArrayBit ManipulationSimulation5 min readAsked at: Microsoft, Meta
Practice this problem

Problem Statement

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.

 

Example 1:

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

Output: 6

Explanation:

The even numbers are 2, 4, and 6. Their bitwise OR equals 6.

Example 2:

Input: nums = [7,9,11]

Output: 0

Explanation:

There are no even numbers, so the result is 0.

Example 3:

Input: nums = [1,8,16]

Output: 24

Explanation:

The even numbers are 8 and 16. Their bitwise OR equals 24.

 

Constraints:

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100

Approach Overview

Problem Overview: Given an integer array, compute the bitwise OR of only the even numbers. Odd values are ignored. If the array contains multiple even numbers, combine them using the bitwise OR operator to produce the final result.

Approach 1: Filter Even Numbers Then OR (Two-Pass) (Time: O(n), Space: O(n))

Scan the array and collect all even numbers into a temporary list. A number is even if num % 2 == 0 or (num & 1) == 0. After building this list, iterate over it and accumulate the result using the bitwise OR operation: result |= value. This approach separates filtering and aggregation, which can make the logic easier to reason about during early implementation. The tradeoff is extra memory because the filtered list stores up to n elements.

Approach 2: Single-Pass Simulation (Time: O(n), Space: O(1))

Traverse the array once and maintain a running OR value. For each element, check if it is even using a bit test like (num & 1) == 0. When the condition holds, merge the value into the accumulator with result |= num. This works because bitwise OR is associative and order independent, so the result is identical regardless of the processing order. The algorithm performs one pass and keeps only a single integer state, making it optimal for both time and space. This pattern appears frequently in problems involving array traversal and bit manipulation, and the implementation is essentially a straightforward simulation of the required operation.

Recommended for interviews: The single-pass simulation is the expected solution. It shows you understand how to filter elements while aggregating a bitwise result in one traversal. Mentioning the two-pass filtering approach first demonstrates basic reasoning, but the O(n) time and O(1) space simulation is the cleanest and most efficient implementation.

Solution

We define a variable ans with an initial value of 0. Then, we iterate through each element x in the array nums; if x is even, we update ans with the bitwise OR of ans and x.

Finally, we return ans.

The time complexity is O(n), where n is the length of the array nums. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Filter Even Numbers Then OR (Two-Pass)O(n)O(n)When separating filtering and aggregation improves readability or debugging
Single-Pass SimulationO(n)O(1)General case and interview-preferred solution for linear scans

Video Solution

Bitwise OR of Even Numbers in an Array | Weekly Contest 468 | Java Code | Developer Coder • Developer Coder • 483 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Bitwise OR of Even Numbers in an Array easy or hard?
This problem is considered Easy because it relies on basic array traversal and a simple bitwise operation. The key idea is recognizing that you only process even numbers and accumulate the result using the OR operator in a single pass.
Bitwise OR of Even Numbers in an Array Python/Java solution
The implementation is identical across languages: iterate through the array, check if the value is even, and apply result |= value. Python, Java, C++, Go, and TypeScript all support the same bitwise OR operator, making the logic straightforward to port between languages.
How to solve Bitwise OR of Even Numbers in an Array in O(n)?
Iterate through the array and keep a variable called result initialized to 0. For each element, check if it is even using (num & 1) == 0. If it is even, update the accumulator with result |= num. After the traversal finishes, result contains the OR of all even numbers.
What is the best approach for Bitwise OR of Even Numbers in an Array?
The best approach is a single-pass simulation that scans the array once and maintains a running bitwise OR value. For every number, check if it is even using (num & 1) == 0 and merge it with result |= num. This runs in O(n) time with O(1) extra space.
Is Bitwise OR of Even Numbers in an Array asked at Google/Amazon/Meta?
Problems involving bitwise operations and array traversal frequently appear in interviews at companies like Google, Amazon, and Meta. While this exact question may vary, the pattern of filtering elements and aggregating with bitwise operators is common in coding interviews.
What data structure is used in Bitwise OR of Even Numbers in an Array?
The primary data structure is a simple array that stores the input integers. The optimal algorithm does not require additional structures beyond a single integer accumulator used to compute the running bitwise OR.
What is the time complexity of Bitwise OR of Even Numbers in an Array?
The optimal solution runs in O(n) time because each element in the array is visited exactly once. The algorithm performs a constant-time parity check and a bitwise OR operation per element. Space complexity is O(1) since only a single accumulator variable is maintained.

Ready to solve this problem?

Practice Bitwise OR of Even Numbers in an Array with our built-in code editor and test cases.

Practice on FleetCode