[LeetCode] Check if Bitwise OR Has Trailing Zeros

10031. Check if Bitwise OR Has Trailing Zeros

You are given an array of positive integers nums.

You have to check if it is possible to select two or more elements in the array such that the bitwise OR of the selected elements has at least one trailing zero in its binary representation.

For example, the binary representation of 5, which is "101", does not have any trailing zeros, whereas the binary representation of 4, which is "100", has two trailing zeros.

Return true if it is possible to select two or more elements whose bitwise OR has trailing zeros, return false otherwise.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
bool hasTrailingZeros(vector<int>& nums) {
int cnt = 0;
for(auto& x : nums) {
if(x % 2 == 0) cnt += 1;
}
return cnt >= 2;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2023/12/31/PS/LeetCode/check-if-bitwise-or-has-trailing-zeros/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.