{"id":10089,"date":"2023-10-29T18:12:57","date_gmt":"2023-10-30T01:12:57","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10089"},"modified":"2023-10-29T18:14:03","modified_gmt":"2023-10-30T01:14:03","slug":"leetcode-2917-find-the-k-or-of-an-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/uncategorized\/leetcode-2917-find-the-k-or-of-an-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2917. Find the K-or of an Array"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>, and an integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<strong>K-or<\/strong>&nbsp;of&nbsp;<code>nums<\/code>&nbsp;is a non-negative integer that satisfies the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;bit is set in the K-or&nbsp;<strong>if and only if<\/strong>&nbsp;there are at least&nbsp;<code>k<\/code>&nbsp;elements of nums in which bit&nbsp;<code>i<\/code>&nbsp;is set.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>K-or<\/strong>&nbsp;of<\/em>&nbsp;<code>nums<\/code>.<\/p>\n\n\n\n<p><strong>Note<\/strong>&nbsp;that a bit&nbsp;<code>i<\/code>&nbsp;is set in&nbsp;<code>x<\/code>&nbsp;if&nbsp;<code>(2<sup>i<\/sup> AND x) == 2<sup>i<\/sup><\/code>, where&nbsp;<code>AND<\/code>&nbsp;is the bitwise&nbsp;<code>AND<\/code>&nbsp;operator.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [7,12,9,8,9,15], k = 4\n<strong>Output:<\/strong> 9\n<strong>Explanation:<\/strong> Bit 0 is set at nums[0], nums[2], nums[4], and nums[5].\nBit 1 is set at nums[0], and nums[5].\nBit 2 is set at nums[0], nums[1], and nums[5].\nBit 3 is set at nums[1], nums[2], nums[3], nums[4], and nums[5].\nOnly bits 0 and 3 are set in at least k elements of the array, and bits i &gt;= 4 are not set in any of the array's elements. Hence, the answer is 2^0 + 2^3 = 9.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [2,12,1,11,4,5], k = 6\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> Since k == 6 == nums.length, the 6-or of the array is equal to the bitwise AND of all its elements. Hence, the answer is 2 AND 12 AND 1 AND 11 AND 4 AND 5 = 0.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [10,8,5,9,11,6,8], k = 1\n<strong>Output:<\/strong> 15\n<strong>Explanation:<\/strong> 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.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 50<\/code><\/li><li><code>0 &lt;= nums[i] &lt; 2<sup>31<\/sup><\/code><\/li><li><code>1 &lt;= k &lt;= nums.length<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Bit Operation<\/strong><\/h2>\n\n\n\n<p>Enumerate every bit and enumerate every number.<\/p>\n\n\n\n<p>Time complexity: O(31 * n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int findKOr(vector<int>& nums, int k) {\n    int ans = 0;\n    for (int i = 0; i < 31; ++i) {\n      int count = 0;\n      for (int x : nums)\n        if (x >> i & 1) ++count;\n      if (count >= k) ans |= 1 << i;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums, and an integer&nbsp;k. The&nbsp;K-or&nbsp;of&nbsp;nums&nbsp;is a non-negative integer that satisfies the following: The&nbsp;ith&nbsp;bit is set in the K-or&nbsp;if and only if&nbsp;there&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10089","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=10089"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10089\/revisions"}],"predecessor-version":[{"id":10092,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10089\/revisions\/10092"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}