返回介绍

solution / 2900-2999 / 2980.Check if Bitwise OR Has Trailing Zeros / README_EN

发布于 2024-06-17 01:02:58 字数 4135 浏览 0 评论 0 收藏 0

2980. Check if Bitwise OR Has Trailing Zeros

中文文档

Description

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_.

 

Example 1:

Input: nums = [1,2,3,4,5]
Output: true
Explanation: If we select the elements 2 and 4, their bitwise OR is 6, which has the binary representation "110" with one trailing zero.

Example 2:

Input: nums = [2,4,8,16]
Output: true
Explanation: If we select the elements 2 and 4, their bitwise OR is 6, 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).

Example 3:

Input: nums = [1,3,5,7,9]
Output: false
Explanation: There is no possible way to select two or more elements to have trailing zeros in the binary representation of their bitwise OR.

 

Constraints:

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

Solutions

Solution 1: Counting Even Numbers

According to the problem statement, if there are two or more elements in the array whose bitwise OR operation results in trailing zeros, then there must be at least two even numbers in the array. Therefore, we can count the number of even numbers in the array. If the count of even numbers is greater than or equal to $2$, then return true, otherwise return false.

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

class Solution:
  def hasTrailingZeros(self, nums: List[int]) -> bool:
    return sum(x & 1 ^ 1 for x in nums) >= 2
class Solution {
  public boolean hasTrailingZeros(int[] nums) {
    int cnt = 0;
    for (int x : nums) {
      cnt += (x & 1 ^ 1);
    }
    return cnt >= 2;
  }
}
class Solution {
public:
  bool hasTrailingZeros(vector<int>& nums) {
    int cnt = 0;
    for (int x : nums) {
      cnt += (x & 1 ^ 1);
    }
    return cnt >= 2;
  }
};
func hasTrailingZeros(nums []int) bool {
  cnt := 0
  for _, x := range nums {
    cnt += (x&1 ^ 1)
  }
  return cnt >= 2
}
function hasTrailingZeros(nums: number[]): boolean {
  let cnt = 0;
  for (const x of nums) {
    cnt += (x & 1) ^ 1;
  }
  return cnt >= 2;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文