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 trueif it is possible to select two or more elements whose bitwiseORhas trailing zeros, returnfalseotherwise.
Input: nums =[1,2,3,4,5]Output: trueExplanation: If we select the elements 2 and 4, their bitwise OR is6, which has the binary representation "110"with one trailing zero.
Input: nums =[2,4,8,16]Output: trueExplanation: If we select the elements 2 and 4, their bitwise OR is6, which has the binary representation "110"with one trailing zero.Other possible ways to select elements to have trailing zeroes in the binary representation of their bitwise OR are:(2,8),(2,16),(4,8),(4,16),(8,16),(2,4,8),(2,4,16),(2,8,16),(4,8,16), and (2,4,8,16).
Input: nums =[1,3,5,7,9]Output: falseExplanation: There is no possible way to select two or more elements to have trailing zeros in the binary representation of their bitwise OR.
A number has a trailing zero in binary if it is even. The bitwise OR of two numbers will have a trailing zero if at least one of the numbers has a trailing zero (i.e., is even), and there are at least two numbers. So, if there are at least two even numbers, or at least one even and any other number, the answer is true. But, if all numbers are odd, their OR will always be odd (no trailing zero). So, we just need to check if there are at least two numbers and at least one even number.
classSolution {
public:bool hasTrailingZeros(vector<int>& nums) {
int even =0;
for (int x : nums) if (x %2==0) even++;
return even >=1&& nums.size() >=2;
}
};
classSolution {
publicbooleanhasTrailingZeros(int[] nums) {
int even = 0;
for (int x : nums) if (x % 2 == 0) even++;
return even >= 1 && nums.length>= 2;
}
}
1
2
3
4
5
6
7
classSolution {
funhasTrailingZeros(nums: IntArray): Boolean {
var even = 0for (x in nums) if (x % 2==0) even++return even >=1&& nums.size >=2 }
}
1
2
3
4
classSolution:
defhasTrailingZeros(self, nums: list[int]) -> bool:
even = sum(x %2==0for x in nums)
return even >=1and len(nums) >=2
1
2
3
4
5
6
impl Solution {
pubfnhas_trailing_zeros(nums: Vec<i32>) -> bool {
let even = nums.iter().filter(|&&x| x %2==0).count();
even >=1&& nums.len() >=2 }
}