Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 <= 1001 <= nums[i] <= 100Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Bit Manipulation
[1,2,3,4,5,6]
[7,9,11]
[1,8,16]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3688: Bitwise OR of Even Numbers in an Array
class Solution {
public int evenNumberBitwiseORs(int[] nums) {
int ans = 0;
for (int x : nums) {
if (x % 2 == 0) {
ans |= x;
}
}
return ans;
}
}
// Accepted solution for LeetCode #3688: Bitwise OR of Even Numbers in an Array
func evenNumberBitwiseORs(nums []int) (ans int) {
for _, x := range nums {
if x%2 == 0 {
ans |= x
}
}
return
}
# Accepted solution for LeetCode #3688: Bitwise OR of Even Numbers in an Array
class Solution:
def evenNumberBitwiseORs(self, nums: List[int]) -> int:
return reduce(or_, (x for x in nums if x % 2 == 0), 0)
// Accepted solution for LeetCode #3688: Bitwise OR of Even Numbers in an Array
fn even_number_bitwise_o_rs(nums: Vec<i32>) -> i32 {
nums.into_iter()
.filter(|&n| n % 2 == 0)
.fold(0, |acc, n| acc | n)
}
fn main() {
let ret = even_number_bitwise_o_rs(vec![1, 8, 16]);
println!("ret={ret}");
}
#[test]
fn test() {
assert_eq!(even_number_bitwise_o_rs(vec![1, 2, 3, 4, 5, 6]), 6);
assert_eq!(even_number_bitwise_o_rs(vec![7, 9, 11]), 0);
assert_eq!(even_number_bitwise_o_rs(vec![1, 8, 16]), 24);
}
// Accepted solution for LeetCode #3688: Bitwise OR of Even Numbers in an Array
function evenNumberBitwiseORs(nums: number[]): number {
return nums.reduce((ans, x) => (x % 2 === 0 ? ans | x : ans), 0);
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.