From 1eed2a4d5b9af09607bdc9baf029d6ff281e3977 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Tue, 15 Apr 2025 07:02:49 +0530 Subject: [PATCH 1/5] Create 1318. Minimum Flips to Make a OR b Equal to c.py Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com> --- .../1318. Minimum Flips to Make a OR b Equal to c.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Solution/1318. Minimum Flips to Make a OR b Equal to c/1318. Minimum Flips to Make a OR b Equal to c.py diff --git a/Solution/1318. Minimum Flips to Make a OR b Equal to c/1318. Minimum Flips to Make a OR b Equal to c.py b/Solution/1318. Minimum Flips to Make a OR b Equal to c/1318. Minimum Flips to Make a OR b Equal to c.py new file mode 100644 index 0000000..e69de29 From 2b4f46494a0de3864843ae6889d7c839e1f33e48 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Tue, 15 Apr 2025 07:02:57 +0530 Subject: [PATCH 2/5] Update 1318. Minimum Flips to Make a OR b Equal to c.py Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com> --- .../1318. Minimum Flips to Make a OR b Equal to c.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Solution/1318. Minimum Flips to Make a OR b Equal to c/1318. Minimum Flips to Make a OR b Equal to c.py b/Solution/1318. Minimum Flips to Make a OR b Equal to c/1318. Minimum Flips to Make a OR b Equal to c.py index e69de29..9cbc707 100644 --- a/Solution/1318. Minimum Flips to Make a OR b Equal to c/1318. Minimum Flips to Make a OR b Equal to c.py +++ b/Solution/1318. Minimum Flips to Make a OR b Equal to c/1318. Minimum Flips to Make a OR b Equal to c.py @@ -0,0 +1,7 @@ +class Solution: + def minFlips(self, a: int, b: int, c: int) -> int: + ans = 0 + for i in range(32): + x, y, z = a >> i & 1, b >> i & 1, c >> i & 1 + ans += x + y if z == 0 else int(x == 0 and y == 0) + return ans \ No newline at end of file From 5d7b12a5eb75b59a46e1688f2e1c0a18033ea2c7 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Tue, 15 Apr 2025 07:03:17 +0530 Subject: [PATCH 3/5] Create readme.md Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com> --- Solution/1318. Minimum Flips to Make a OR b Equal to c/readme.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Solution/1318. Minimum Flips to Make a OR b Equal to c/readme.md diff --git a/Solution/1318. Minimum Flips to Make a OR b Equal to c/readme.md b/Solution/1318. Minimum Flips to Make a OR b Equal to c/readme.md new file mode 100644 index 0000000..e69de29 From 7493439a37d3af266a733544641d1a924627b38c Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Tue, 15 Apr 2025 07:03:24 +0530 Subject: [PATCH 4/5] Update readme.md Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com> --- .../readme.md | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) diff --git a/Solution/1318. Minimum Flips to Make a OR b Equal to c/readme.md b/Solution/1318. Minimum Flips to Make a OR b Equal to c/readme.md index e69de29..514578d 100644 --- a/Solution/1318. Minimum Flips to Make a OR b Equal to c/readme.md +++ b/Solution/1318. Minimum Flips to Make a OR b Equal to c/readme.md @@ -0,0 +1,147 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/1300-1399/1318.Minimum%20Flips%20to%20Make%20a%20OR%20b%20Equal%20to%20c/README_EN.md +rating: 1382 +source: Weekly Contest 171 Q2 +tags: + - Bit Manipulation +--- + + + +# [1318. Minimum Flips to Make a OR b Equal to c](https://leetcode.com/problems/minimum-flips-to-make-a-or-b-equal-to-c) + +[中文文档](/solution/1300-1399/1318.Minimum%20Flips%20to%20Make%20a%20OR%20b%20Equal%20to%20c/README.md) + +## Description + + + +

Given 3 positives numbers a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b == c ). (bitwise OR operation).
+Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation.

+ +

 

+

Example 1:

+ +

+ +
+Input: a = 2, b = 6, c = 5
+Output: 3
+Explanation: After flips a = 1 , b = 4 , c = 5 such that (a OR b == c)
+ +

Example 2:

+ +
+Input: a = 4, b = 2, c = 7
+Output: 1
+
+ +

Example 3:

+ +
+Input: a = 1, b = 2, c = 3
+Output: 0
+
+ +

 

+

Constraints:

+ + + + + +## Solutions + + + +### Solution 1: Bit Manipulation + +We can enumerate each bit of the binary representation of $a$, $b$, and $c$, denoted as $x$, $y$, and $z$ respectively. If the bitwise OR operation result of $x$ and $y$ is different from $z$, we then check if both $x$ and $y$ are $1$. If so, we need to flip twice, otherwise, we only need to flip once. We accumulate all the required flip times. + +The time complexity is $O(\log M)$, where $M$ is the maximum value of the numbers in the problem. The space complexity is $O(1)$. + + + +#### Python3 + +```python +class Solution: + def minFlips(self, a: int, b: int, c: int) -> int: + ans = 0 + for i in range(32): + x, y, z = a >> i & 1, b >> i & 1, c >> i & 1 + ans += x + y if z == 0 else int(x == 0 and y == 0) + return ans +``` + +#### Java + +```java +class Solution { + public int minFlips(int a, int b, int c) { + int ans = 0; + for (int i = 0; i < 32; ++i) { + int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1; + ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0); + } + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int minFlips(int a, int b, int c) { + int ans = 0; + for (int i = 0; i < 32; ++i) { + int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1; + ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0); + } + return ans; + } +}; +``` + +#### Go + +```go +func minFlips(a int, b int, c int) (ans int) { + for i := 0; i < 32; i++ { + x, y, z := a>>i&1, b>>i&1, c>>i&1 + if z == 0 { + ans += x + y + } else if x == 0 && y == 0 { + ans++ + } + } + return +} +``` + +#### TypeScript + +```ts +function minFlips(a: number, b: number, c: number): number { + let ans = 0; + for (let i = 0; i < 32; ++i) { + const [x, y, z] = [(a >> i) & 1, (b >> i) & 1, (c >> i) & 1]; + ans += z === 0 ? x + y : x + y === 0 ? 1 : 0; + } + return ans; +} +``` + + + + + + \ No newline at end of file From 1c018f21d251dcc288307434c7ac68e325eb9aac Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Tue, 15 Apr 2025 07:04:09 +0530 Subject: [PATCH 5/5] Update readme.md Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com> --- .../readme.md | 32 +------------------ 1 file changed, 1 insertion(+), 31 deletions(-) diff --git a/Solution/1318. Minimum Flips to Make a OR b Equal to c/readme.md b/Solution/1318. Minimum Flips to Make a OR b Equal to c/readme.md index 514578d..446a59a 100644 --- a/Solution/1318. Minimum Flips to Make a OR b Equal to c/readme.md +++ b/Solution/1318. Minimum Flips to Make a OR b Equal to c/readme.md @@ -1,7 +1,7 @@ --- comments: true difficulty: Medium -edit_url: https://github.com/doocs/leetcode/edit/main/solution/1300-1399/1318.Minimum%20Flips%20to%20Make%20a%20OR%20b%20Equal%20to%20c/README_EN.md +edit_url: antim rating: 1382 source: Weekly Contest 171 Q2 tags: @@ -12,7 +12,6 @@ tags: # [1318. Minimum Flips to Make a OR b Equal to c](https://leetcode.com/problems/minimum-flips-to-make-a-or-b-equal-to-c) -[中文文档](/solution/1300-1399/1318.Minimum%20Flips%20to%20Make%20a%20OR%20b%20Equal%20to%20c/README.md) ## Description @@ -111,35 +110,6 @@ public: }; ``` -#### Go - -```go -func minFlips(a int, b int, c int) (ans int) { - for i := 0; i < 32; i++ { - x, y, z := a>>i&1, b>>i&1, c>>i&1 - if z == 0 { - ans += x + y - } else if x == 0 && y == 0 { - ans++ - } - } - return -} -``` - -#### TypeScript - -```ts -function minFlips(a: number, b: number, c: number): number { - let ans = 0; - for (let i = 0; i < 32; ++i) { - const [x, y, z] = [(a >> i) & 1, (b >> i) & 1, (c >> i) & 1]; - ans += z === 0 ? x + y : x + y === 0 ? 1 : 0; - } - return ans; -} -``` -