Skip to content

Commit 02fd35c

Browse files
authored
Create weekly-contest-468-q1-bitwise-or-of-even-numbers-in-an-array.java
1 parent 1b666f1 commit 02fd35c

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Q1. Bitwise OR of Even Numbers in an Array
3+
* Easy - 3 pt
4+
*
5+
* You are given an integer array nums.
6+
* Return the bitwise OR of all even numbers in the array.
7+
* If there are no even numbers in nums, return 0.
8+
*
9+
* Example 1:
10+
* Input: nums = [1,2,3,4,5,6]
11+
* Output: 6
12+
* Explanation: The even numbers are 2, 4, and 6. Their bitwise OR equals 6.
13+
*
14+
* Example 2:
15+
* Input: nums = [1,3,5]
16+
* Output: 0
17+
* Explanation: There are no even numbers in nums.
18+
*
19+
* ------------------------------------------------------------------
20+
* Approach:
21+
* - Initialize result as 0.
22+
* - Iterate through each number in nums.
23+
* - If the number is even, perform bitwise OR with result.
24+
* - Return the final result.
25+
*
26+
* ------------------------------------------------------------------
27+
* Time Complexity: O(n)
28+
* (We traverse the array once)
29+
*
30+
* Space Complexity: O(1)
31+
* (We use only a single integer variable for the result)
32+
*
33+
* ------------------------------------------------------------------
34+
* LeetCode link: https://leetcode.com/problems/bitwise-or-of-even-numbers-in-an-array/
35+
*/
36+
37+
class Solution {
38+
public int evenNumberBitwiseORs(int[] nums) {
39+
int res = 0;
40+
for (int num : nums) {
41+
if (num % 2 == 0) {
42+
res |= num;
43+
}
44+
}
45+
return res;
46+
}
47+
}

0 commit comments

Comments
 (0)