{"id":4695,"date":"2019-01-26T22:26:03","date_gmt":"2019-01-27T06:26:03","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4695"},"modified":"2019-01-27T13:47:17","modified_gmt":"2019-01-27T21:47:17","slug":"leetcode-982-triples-with-bitwise-and-equal-to-zero","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-982-triples-with-bitwise-and-equal-to-zero\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 982. Triples with Bitwise AND Equal To Zero"},"content":{"rendered":"\n<p>Given an array of integers&nbsp;<code>A<\/code>, find the number of&nbsp;triples of indices (i, j, k)&nbsp;such that:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= i &lt; A.length<\/code><\/li><li><code>0 &lt;= j &lt; A.length<\/code><\/li><li><code>0 &lt;= k &lt; A.length<\/code><\/li><li><code>A[i]&nbsp;&amp; A[j]&nbsp;&amp; A[k] == 0<\/code>, where&nbsp;<code>&amp;<\/code>&nbsp;represents the bitwise-AND operator.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[2,1,3]\n<strong>Output: <\/strong>12\n<strong>Explanation: <\/strong>We could choose the following i, j, k triples:\n(i=0, j=0, k=1) : 2 &amp; 2 &amp; 1\n(i=0, j=1, k=0) : 2 &amp; 1 &amp; 2\n(i=0, j=1, k=1) : 2 &amp; 1 &amp; 1\n(i=0, j=1, k=2) : 2 &amp; 1 &amp; 3\n(i=0, j=2, k=1) : 2 &amp; 3 &amp; 1\n(i=1, j=0, k=0) : 1 &amp; 2 &amp; 2\n(i=1, j=0, k=1) : 1 &amp; 2 &amp; 1\n(i=1, j=0, k=2) : 1 &amp; 2 &amp; 3\n(i=1, j=1, k=0) : 1 &amp; 1 &amp; 2\n(i=1, j=2, k=0) : 1 &amp; 3 &amp; 2\n(i=2, j=0, k=1) : 3 &amp; 2 &amp; 1\n(i=2, j=1, k=0) : 3 &amp; 1 &amp; 2\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= A.length &lt;= 1000<\/code><\/li><li><code>0 &lt;= A[i] &lt; 2^16<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Solution: Counting<\/h2>\n\n\n\n<p>Time complexity: O(n^2 + n * max(A))<br>Space complexity: O(max(A))<\/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, running time: 60 ms\nclass Solution {\npublic:\n  int countTriplets(vector<int>& A) {\n    const int n = *max_element(begin(A), end(A));\n    vector<int> count(n + 1);\n    for (int a: A)\n      for (int b: A)\n        ++count[a & b];\n    int ans = 0;\n    for (int a : A)\n      for (int i = 0; i <= n; ++i)\n        if ((a &#038; i) == 0) ans += count[i];\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers&nbsp;A, find the number of&nbsp;triples of indices (i, j, k)&nbsp;such that: 0 &lt;= i &lt; A.length 0 &lt;= j &lt; A.length&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[20,8,217,82],"class_list":["post-4695","post","type-post","status-publish","format-standard","hentry","category-bit","tag-array","tag-counting","tag-hard","tag-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4695","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=4695"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4695\/revisions"}],"predecessor-version":[{"id":4698,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4695\/revisions\/4698"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}