{"id":8610,"date":"2021-10-17T20:54:32","date_gmt":"2021-10-18T03:54:32","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8610"},"modified":"2021-10-17T20:55:20","modified_gmt":"2021-10-18T03:55:20","slug":"leetcode-2044-count-number-of-maximum-bitwise-or-subsets","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-2044-count-number-of-maximum-bitwise-or-subsets\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2044. Count Number of Maximum Bitwise-OR Subsets"},"content":{"rendered":"\n<p>Given an integer array&nbsp;<code>nums<\/code>, find the&nbsp;<strong>maximum<\/strong>&nbsp;possible&nbsp;<strong>bitwise OR<\/strong>&nbsp;of a subset of&nbsp;<code>nums<\/code>&nbsp;and return&nbsp;<em>the&nbsp;<strong>number of different non-empty subsets<\/strong>&nbsp;with the maximum bitwise OR<\/em>.<\/p>\n\n\n\n<p>An array&nbsp;<code>a<\/code>&nbsp;is a&nbsp;<strong>subset<\/strong>&nbsp;of an array&nbsp;<code>b<\/code>&nbsp;if&nbsp;<code>a<\/code>&nbsp;can be obtained from&nbsp;<code>b<\/code>&nbsp;by deleting some (possibly zero) elements of&nbsp;<code>b<\/code>. Two subsets are considered&nbsp;<strong>different<\/strong>&nbsp;if the indices of the elements chosen are different.<\/p>\n\n\n\n<p>The bitwise OR of an array&nbsp;<code>a<\/code>&nbsp;is equal to&nbsp;<code>a[0]&nbsp;<strong>OR<\/strong>&nbsp;a[1]&nbsp;<strong>OR<\/strong>&nbsp;...&nbsp;<strong>OR<\/strong>&nbsp;a[a.length - 1]<\/code>&nbsp;(<strong>0-indexed<\/strong>).<\/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 = [3,1]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The maximum possible bitwise OR of a subset is 3. There are 2 subsets with a bitwise OR of 3:\n- [3]\n- [3,1]\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,2,2]\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> All non-empty subsets of [2,2,2] have a bitwise OR of 2. There are 2<sup>3<\/sup> - 1 = 7 total subsets.\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 = [3,2,1,5]\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> The maximum possible bitwise OR of a subset is 7. There are 6 subsets with a bitwise OR of 7:\n- [3,5]\n- [3,1,5]\n- [3,2,5]\n- [3,2,1,5]\n- [2,5]\n- [2,1,5]<\/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;= 16<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Try all possible subsets<\/p>\n\n\n\n<p>Time complexity: O(n*2<sup>n<\/sup>)<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 countMaxOrSubsets(vector<int>& nums) {\n    const int n = nums.size();\n    int max_or = 0;\n    int count = 0;\n    for (int s = 0; s < 1 << n; ++s) {\n      int cur_or = 0;\n      for (int i = 0; i < n; ++i)\n        if (s >> i & 1) cur_or |= nums[i];\n      if (cur_or > max_or) {\n        max_or = cur_or;\n        count = 1;\n      } else if (cur_or == max_or) {\n        ++count;\n      }\n    }\n    return count;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;nums, find the&nbsp;maximum&nbsp;possible&nbsp;bitwise OR&nbsp;of a subset of&nbsp;nums&nbsp;and return&nbsp;the&nbsp;number of different non-empty subsets&nbsp;with the maximum bitwise OR. An array&nbsp;a&nbsp;is a&nbsp;subset&nbsp;of an array&nbsp;b&nbsp;if&nbsp;a&nbsp;can be&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[122,177,493],"class_list":["post-8610","post","type-post","status-publish","format-standard","hentry","category-searching","tag-combination","tag-medium","tag-subset","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8610","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=8610"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8610\/revisions"}],"predecessor-version":[{"id":8612,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8610\/revisions\/8612"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}