Skip to content

Commit 7493439

Browse files
iamAntimPalAntim-IWPIamShiwangi
committed
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>
1 parent 5d7b12a commit 7493439

File tree

1 file changed

+147
-0
lines changed
  • Solution/1318. Minimum Flips to Make a OR b Equal to c

1 file changed

+147
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
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
5+
rating: 1382
6+
source: Weekly Contest 171 Q2
7+
tags:
8+
- Bit Manipulation
9+
---
10+
11+
<!-- problem:start -->
12+
13+
# [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)
14+
15+
[中文文档](/solution/1300-1399/1318.Minimum%20Flips%20to%20Make%20a%20OR%20b%20Equal%20to%20c/README.md)
16+
17+
## Description
18+
19+
<!-- description:start -->
20+
21+
<p>Given 3 positives numbers <code>a</code>, <code>b</code> and <code>c</code>. Return the minimum flips required in some bits of <code>a</code> and <code>b</code> to make (&nbsp;<code>a</code> OR <code>b</code> == <code>c</code>&nbsp;). (bitwise OR operation).<br />
22+
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>
23+
24+
<p>&nbsp;</p>
25+
<p><strong class="example">Example 1:</strong></p>
26+
27+
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1318.Minimum%20Flips%20to%20Make%20a%20OR%20b%20Equal%20to%20c/images/sample_3_1676.png" style="width: 260px; height: 87px;" /></p>
28+
29+
<pre>
30+
<strong>Input:</strong> a = 2, b = 6, c = 5
31+
<strong>Output:</strong> 3
32+
<strong>Explanation: </strong>After flips a = 1 , b = 4 , c = 5 such that (<code>a</code> OR <code>b</code> == <code>c</code>)</pre>
33+
34+
<p><strong class="example">Example 2:</strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong> a = 4, b = 2, c = 7
38+
<strong>Output:</strong> 1
39+
</pre>
40+
41+
<p><strong class="example">Example 3:</strong></p>
42+
43+
<pre>
44+
<strong>Input:</strong> a = 1, b = 2, c = 3
45+
<strong>Output:</strong> 0
46+
</pre>
47+
48+
<p>&nbsp;</p>
49+
<p><strong>Constraints:</strong></p>
50+
51+
<ul>
52+
<li><code>1 &lt;= a &lt;= 10^9</code></li>
53+
<li><code>1 &lt;= b&nbsp;&lt;= 10^9</code></li>
54+
<li><code>1 &lt;= c&nbsp;&lt;= 10^9</code></li>
55+
</ul>
56+
57+
<!-- description:end -->
58+
59+
## Solutions
60+
61+
<!-- solution:start -->
62+
63+
### Solution 1: Bit Manipulation
64+
65+
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.
66+
67+
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)$.
68+
69+
<!-- tabs:start -->
70+
71+
#### Python3
72+
73+
```python
74+
class Solution:
75+
def minFlips(self, a: int, b: int, c: int) -> int:
76+
ans = 0
77+
for i in range(32):
78+
x, y, z = a >> i & 1, b >> i & 1, c >> i & 1
79+
ans += x + y if z == 0 else int(x == 0 and y == 0)
80+
return ans
81+
```
82+
83+
#### Java
84+
85+
```java
86+
class Solution {
87+
public int minFlips(int a, int b, int c) {
88+
int ans = 0;
89+
for (int i = 0; i < 32; ++i) {
90+
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
91+
ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
92+
}
93+
return ans;
94+
}
95+
}
96+
```
97+
98+
#### C++
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
int minFlips(int a, int b, int c) {
104+
int ans = 0;
105+
for (int i = 0; i < 32; ++i) {
106+
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
107+
ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
108+
}
109+
return ans;
110+
}
111+
};
112+
```
113+
114+
#### Go
115+
116+
```go
117+
func minFlips(a int, b int, c int) (ans int) {
118+
for i := 0; i < 32; i++ {
119+
x, y, z := a>>i&1, b>>i&1, c>>i&1
120+
if z == 0 {
121+
ans += x + y
122+
} else if x == 0 && y == 0 {
123+
ans++
124+
}
125+
}
126+
return
127+
}
128+
```
129+
130+
#### TypeScript
131+
132+
```ts
133+
function minFlips(a: number, b: number, c: number): number {
134+
let ans = 0;
135+
for (let i = 0; i < 32; ++i) {
136+
const [x, y, z] = [(a >> i) & 1, (b >> i) & 1, (c >> i) & 1];
137+
ans += z === 0 ? x + y : x + y === 0 ? 1 : 0;
138+
}
139+
return ans;
140+
}
141+
```
142+
143+
<!-- tabs:end -->
144+
145+
<!-- solution:end -->
146+
147+
<!-- problem:end -->

0 commit comments

Comments
 (0)