Skip to content

Commit f990758

Browse files
authored
Merge pull request #21 from iamAntimPal/Leetcode-75
Leetcode 75
2 parents 454ea2e + 1c018f2 commit f990758

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def minFlips(self, a: int, b: int, c: int) -> int:
3+
ans = 0
4+
for i in range(32):
5+
x, y, z = a >> i & 1, b >> i & 1, c >> i & 1
6+
ans += x + y if z == 0 else int(x == 0 and y == 0)
7+
return ans
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
edit_url: antim
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+
16+
## Description
17+
18+
<!-- description:start -->
19+
20+
<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 />
21+
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>
22+
23+
<p>&nbsp;</p>
24+
<p><strong class="example">Example 1:</strong></p>
25+
26+
<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>
27+
28+
<pre>
29+
<strong>Input:</strong> a = 2, b = 6, c = 5
30+
<strong>Output:</strong> 3
31+
<strong>Explanation: </strong>After flips a = 1 , b = 4 , c = 5 such that (<code>a</code> OR <code>b</code> == <code>c</code>)</pre>
32+
33+
<p><strong class="example">Example 2:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> a = 4, b = 2, c = 7
37+
<strong>Output:</strong> 1
38+
</pre>
39+
40+
<p><strong class="example">Example 3:</strong></p>
41+
42+
<pre>
43+
<strong>Input:</strong> a = 1, b = 2, c = 3
44+
<strong>Output:</strong> 0
45+
</pre>
46+
47+
<p>&nbsp;</p>
48+
<p><strong>Constraints:</strong></p>
49+
50+
<ul>
51+
<li><code>1 &lt;= a &lt;= 10^9</code></li>
52+
<li><code>1 &lt;= b&nbsp;&lt;= 10^9</code></li>
53+
<li><code>1 &lt;= c&nbsp;&lt;= 10^9</code></li>
54+
</ul>
55+
56+
<!-- description:end -->
57+
58+
## Solutions
59+
60+
<!-- solution:start -->
61+
62+
### Solution 1: Bit Manipulation
63+
64+
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.
65+
66+
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)$.
67+
68+
<!-- tabs:start -->
69+
70+
#### Python3
71+
72+
```python
73+
class Solution:
74+
def minFlips(self, a: int, b: int, c: int) -> int:
75+
ans = 0
76+
for i in range(32):
77+
x, y, z = a >> i & 1, b >> i & 1, c >> i & 1
78+
ans += x + y if z == 0 else int(x == 0 and y == 0)
79+
return ans
80+
```
81+
82+
#### Java
83+
84+
```java
85+
class Solution {
86+
public int minFlips(int a, int b, int c) {
87+
int ans = 0;
88+
for (int i = 0; i < 32; ++i) {
89+
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
90+
ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
91+
}
92+
return ans;
93+
}
94+
}
95+
```
96+
97+
#### C++
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
int minFlips(int a, int b, int c) {
103+
int ans = 0;
104+
for (int i = 0; i < 32; ++i) {
105+
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
106+
ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
107+
}
108+
return ans;
109+
}
110+
};
111+
```
112+
113+
<!-- tabs:end -->
114+
115+
<!-- solution:end -->
116+
117+
<!-- problem:end -->

0 commit comments

Comments
 (0)