{"id":6093,"date":"2020-01-13T21:47:27","date_gmt":"2020-01-14T05:47:27","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6093"},"modified":"2020-01-13T21:50:27","modified_gmt":"2020-01-14T05:50:27","slug":"leetcode-1318-minimum-flips-to-make-a-or-b-equal-to-c","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-1318-minimum-flips-to-make-a-or-b-equal-to-c\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1318. Minimum Flips to Make a OR b Equal to c"},"content":{"rendered":"\n<p>Given 3 positives numbers&nbsp;<code>a<\/code>,&nbsp;<code>b<\/code>&nbsp;and&nbsp;<code>c<\/code>. Return the minimum flips required in some bits of&nbsp;<code>a<\/code>&nbsp;and&nbsp;<code>b<\/code>&nbsp;to make (&nbsp;<code>a<\/code>&nbsp;OR&nbsp;<code>b<\/code>&nbsp;==&nbsp;<code>c<\/code>&nbsp;). (bitwise OR operation).<br>Flip operation&nbsp;consists of change&nbsp;<strong>any<\/strong>&nbsp;single bit 1 to 0 or change the bit 0 to 1&nbsp;in their binary representation.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/01\/06\/sample_3_1676.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> a = 2, b = 6, c = 5\n<strong>Output:<\/strong> 3\n<strong>Explanation: <\/strong>After flips a = 1 , b = 4 , c = 5 such that (<code>a<\/code> OR <code>b<\/code> == <code>c<\/code>)<\/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> a = 4, b = 2, c = 7\n<strong>Output:<\/strong> 1\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> a = 1, b = 2, c = 3\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= a &lt;= 10^9<\/code><\/li><li><code>1 &lt;= b&nbsp;&lt;= 10^9<\/code><\/li><li><code>1 &lt;= c&nbsp;&lt;= 10^9<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Bit operation<\/strong><\/h2>\n\n\n\n<p>If the bit of c is 1, a \/ b at least has one 1. cost = 1 &#8211; ((a | b) &amp; 1)<br>If the bit of c is 0, a \/ b must be 0, cost = (a &amp; 1) + (b &amp; 1)<\/p>\n\n\n\n<p>Time complexity: O(32)<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 minFlips(int a, int b, int c) {\n    int count = 0;\n    for (int i = 0; i < 32; ++i) {      \n      if (c &#038; 1) count += 1 - ((a | b) &#038; 1);\n      else count += (a &#038; 1) + (b &#038; 1);\n      a >>= 1;\n      b >>= 1;\n      c >>= 1;\n    }\n    return count;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given 3 positives numbers&nbsp;a,&nbsp;b&nbsp;and&nbsp;c. Return the minimum flips required in some bits of&nbsp;a&nbsp;and&nbsp;b&nbsp;to make (&nbsp;a&nbsp;OR&nbsp;b&nbsp;==&nbsp;c&nbsp;). (bitwise OR operation).Flip operation&nbsp;consists of change&nbsp;any&nbsp;single bit 1 to 0&#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":[16,530,531,532,529],"class_list":["post-6093","post","type-post","status-publish","format-standard","hentry","category-bit","tag-bit","tag-boolean","tag-medum","tag-o32","tag-or","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6093","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=6093"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6093\/revisions"}],"predecessor-version":[{"id":6095,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6093\/revisions\/6095"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}