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 (+ +aORb==c)
Example 2:
+ ++Input: a = 4, b = 2, c = 7 +Output: 1 ++ +
Example 3:
+ ++Input: a = 1, b = 2, c = 3 +Output: 0 ++ +
+
Constraints:
+ +1 <= a <= 10^91 <= b <= 10^91 <= c <= 10^9