返回介绍

solution / 2900-2999 / 2917.Find the K-or of an Array / README_EN

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

2917. Find the K-or of an Array

中文文档

Description

You are given an integer array nums, and an integer k. Let's introduce K-or operation by extending the standard bitwise OR. In K-or, a bit position in the result is set to 1 if at least k numbers in nums have a 1 in that position.

Return _the K-or of_ nums.

 

Example 1:

Input: nums = [7,12,9,8,9,15], k = 4

Output: 9

Explanation:

Represent numbers in binary:

NumberBit 3Bit 2Bit 1Bit 0
70111
121100
91001
81000
91001
151111
Result = 91001

Bit 0 is set in 7, 9, 9, and 15. Bit 3 is set in 12, 9, 8, 9, and 15.
Only bits 0 and 3 qualify. The result is (1001)2 = 9.

Example 2:

Input: nums = [2,12,1,11,4,5], k = 6

Output: 0

Explanation: No bit appears as 1 in all six array numbers, as required for K-or with k = 6. Thus, the result is 0.

Example 3:

Input: nums = [10,8,5,9,11,6,8], k = 1

Output: 15

Explanation: Since k == 1, the 1-or of the array is equal to the bitwise OR of all its elements. Hence, the answer is 10 OR 8 OR 5 OR 9 OR 11 OR 6 OR 8 = 15.

 

Constraints:

  • 1 <= nums.length <= 50
  • 0 <= nums[i] < 231
  • 1 <= k <= nums.length

Solutions

Solution 1: Enumeration

We can enumerate each bit $i$ in the range $[0, 32)$, and count the number of numbers in the array $nums$ whose $i$-th bit is $1$, denoted as $cnt$. If $cnt \ge k$, we add $2^i$ to the answer.

After the enumeration, we return the answer.

The time complexity is $O(n \times \log M)$, where $n$ and $M$ are the length of the array $nums$ and the maximum value in $nums$, respectively. The space complexity is $O(1)$.

class Solution:
  def findKOr(self, nums: List[int], k: int) -> int:
    ans = 0
    for i in range(32):
      cnt = sum(x >> i & 1 for x in nums)
      if cnt >= k:
        ans |= 1 << i
    return ans
class Solution {
  public int findKOr(int[] nums, int k) {
    int ans = 0;
    for (int i = 0; i < 32; ++i) {
      int cnt = 0;
      for (int x : nums) {
        cnt += (x >> i & 1);
      }
      if (cnt >= k) {
        ans |= 1 << i;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int findKOr(vector<int>& nums, int k) {
    int ans = 0;
    for (int i = 0; i < 32; ++i) {
      int cnt = 0;
      for (int x : nums) {
        cnt += (x >> i & 1);
      }
      if (cnt >= k) {
        ans |= 1 << i;
      }
    }
    return ans;
  }
};
func findKOr(nums []int, k int) (ans int) {
  for i := 0; i < 32; i++ {
    cnt := 0
    for _, x := range nums {
      cnt += (x >> i & 1)
    }
    if cnt >= k {
      ans |= 1 << i
    }
  }
  return
}
function findKOr(nums: number[], k: number): number {
  let ans = 0;
  for (let i = 0; i < 32; ++i) {
    let cnt = 0;
    for (const x of nums) {
      cnt += (x >> i) & 1;
    }
    if (cnt >= k) {
      ans |= 1 << i;
    }
  }
  return ans;
}
public class Solution {
  public int FindKOr(int[] nums, int k) {
    int ans = 0;
    for (int i = 0; i < 32; ++i) {
      int cnt = 0;
      foreach (int x in nums) {
        cnt += (x >> i & 1);
      }
      if (cnt >= k) {
        ans |= 1 << i;
      }
    }
    return ans;
  }
}

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

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

发布评论

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