{"id":523,"date":"2021-09-05T01:41:30","date_gmt":"2021-09-05T01:41:30","guid":{"rendered":"http:\/\/emacslisp.com\/?p=523"},"modified":"2021-09-05T01:41:30","modified_gmt":"2021-09-05T01:41:30","slug":"1863-sum-of-all-subset-xor-totals","status":"publish","type":"post","link":"http:\/\/emacslisp.com\/?p=523","title":{"rendered":"1863. Sum of All Subset XOR Totals"},"content":{"rendered":"<p>key idea is how to generate all subset of array.<\/p>\n<p>take 3 elements of arrays for example,<\/p>\n<p>[a, _, _] -> [1, 0, 0]<br \/>\n[_, b, _] -> [0, 1, 0]<br \/>\n[a, b, _] -> [1, 1, 0]<br \/>\n[_, _, c] -> [0, 0, 1]<br \/>\n[a, _, c] -> [1, 0, 1]<br \/>\n[_, b, c] -> [0, 1, 1]<br \/>\n[a, b, c] -> [1, 1, 1]<\/p>\n<p>integer i is loop through 1 to Math.pow(2, nums.length) &#8211; 1, for each integer, we check how many 1 bit<\/p>\n<p>i &#038; 1, if it is 1, we get nums[index] out as elements for subarray.<\/p>\n<pre lang=\"java\" line=\"1\"> \r\n\r\nclass Solution {\r\n    public int subsetXORSum(int[] nums) {\r\n        int result = 0;\r\n    \tint x = (int)Math.pow(2, nums.length) - 1;\r\n        for(int i=1;i<=x;i++) {\r\n        \tint p = i;\r\n        \tint r = 0;\r\n        \tfor(int j=0;j<nums.length;j++) {\r\n        \t\tint t = p &#038; 1;\r\n        \t\tp >>= 1;\r\n        \t\r\n        \t\tif (t == 1) {\r\n    \t\t\t\tr ^= nums[j];\r\n        \t\t}\r\n        \t}\r\n        \t\r\n        \tresult += r;\r\n        }\r\n        \r\n        return result;\r\n    }\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>key idea is how to generate all subset of array. take 3 elements of arrays for example, [a, _, _] -> [1, 0, 0] [_, b, _] -> [0, 1, 0] [a, b, _] -> [1, 1, 0] [_, _, c] -> [0, 0, 1] [a, _, c] -> [1, 0, 1] [_, b, c] [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,14],"tags":[],"class_list":["post-523","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-java"],"_links":{"self":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=523"}],"version-history":[{"count":1,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/523\/revisions"}],"predecessor-version":[{"id":524,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/523\/revisions\/524"}],"wp:attachment":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=523"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}