Skip to content

Commit 20f6a0b

Browse files
authored
Merge pull request #4270 from nagalakshmi08/2220
Added solution for lc-2220
2 parents 6fe0567 + ec5ca81 commit 20f6a0b

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
---
2+
id: minimum-bit-flips-to-convert-number
3+
title: 2220. Minimum Bit Flips to Convert Number
4+
sidebar_label: 2220. Minimum Bit Flips to Convert Number
5+
tags:
6+
- Bit Manipulation
7+
8+
description: "This is a solution to the 2220. Minimum Bit Flips to Convert Number."
9+
---
10+
11+
## Problem Description
12+
A bit flip of a number x is choosing a bit in the binary representation of x and flipping it from either 0 to 1 or 1 to 0.
13+
14+
For example, for x = 7, the binary representation is 111 and we may choose any bit (including any leading zeros not shown) and flip it. We can flip the first bit from the right to get 110, flip the second bit from the right to get 101, flip the fifth bit from the right (a leading zero) to get 10111, etc.
15+
16+
Given two integers start and goal, return the minimum number of bit flips to convert start to goal.
17+
18+
### Examples
19+
**Example 1:**
20+
```
21+
Input: start = 10, goal = 7
22+
23+
Output: 3
24+
25+
Explanation:
26+
27+
The binary representation of 10 and 7 are 1010 and 0111 respectively. We can convert 10 to 7 in 3 steps:
28+
- Flip the first bit from the right: 1010 -> 1011.
29+
- Flip the third bit from the right: 1011 -> 1111.
30+
- Flip the fourth bit from the right: 1111 -> 0111.
31+
It can be shown we cannot convert 10 to 7 in less than 3 steps. Hence, we return 3.
32+
```
33+
34+
**Example 2:**
35+
```
36+
Input: start = 3, goal = 4
37+
38+
Output: 3
39+
40+
Explanation:
41+
42+
The binary representation of 3 and 4 are 011 and 100 respectively. We can convert 3 to 4 in 3 steps:
43+
- Flip the first bit from the right: 011 -> 010.
44+
- Flip the second bit from the right: 010 -> 000.
45+
- Flip the third bit from the right: 000 -> 100.
46+
It can be shown we cannot convert 3 to 4 in less than 3 steps. Hence, we return 3.
47+
```
48+
49+
### Constraints
50+
- `0 <= start, goal <= 10^9`
51+
## Solution for 2220. Minimum Bit Flips to Convert Number
52+
53+
The problem seems to involve finding the minimum number of bit flips required to transform one integer (start) into another integer (goal).
54+
55+
## Approach
56+
57+
The approach used in the provided code involves XORing the start and goal integers to find the positions where the bits differ. Then, the number of set bits in the result is counted to determine the minimum number of flips needed.
58+
59+
### Code in Different Languages
60+
61+
<Tabs>
62+
<TabItem value="C++" label="C++" default>
63+
<SolutionAuthor name="@nagalakshmi08"/>
64+
65+
```cpp
66+
class Solution {
67+
public:
68+
int minBitFlips(int start, int goal) {
69+
int res=start^goal; // XOR to find the differing bits
70+
int count=0;
71+
while(res){
72+
count+=res&1; // Count the set bits
73+
res>>=1; // Right shift to check next bit
74+
}
75+
return count;
76+
}
77+
};
78+
```
79+
</TabItem>
80+
<TabItem value="Java" label="Java">
81+
<SolutionAuthor name="@nagalakshmi08"/>
82+
```java
83+
class Solution {
84+
public int minBitFlips(int start, int goal) {
85+
int res = start ^ goal; // XOR to find the differing bits
86+
int count = 0;
87+
while (res != 0) {
88+
count += res & 1; // Count the set bits
89+
res >>= 1; // Right shift to check the next bit
90+
}
91+
return count;
92+
}
93+
}
94+
```
95+
96+
</TabItem>
97+
98+
<TabItem value="Python" label="Python">
99+
<SolutionAuthor name="@nagalakshmi08"/>
100+
101+
```python
102+
class Solution:
103+
def minBitFlips(self, start: int, goal: int) -> int:
104+
res = start ^ goal # XOR to find the differing bits
105+
count = 0
106+
while res:
107+
count += res & 1 # Count the set bits
108+
res >>= 1 # Right shift to check the next bit
109+
return count
110+
```
111+
112+
</TabItem>
113+
</Tabs>
114+
115+
#### Complexity Analysis
116+
117+
- **Time Complexity**: The time complexity of this solution is $O(n)$, where n is the number of bits in the integers.
118+
- **Space Complexity**: The space complexity is $O(1)$ because the algorithm only uses a constant amount of extra space.
119+
120+
---
121+
122+
<h2>Authors:</h2>
123+
124+
<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}>
125+
{['nagalakshmi08'].map(username => (
126+
<Author key={username} username={username} />
127+
))}
128+
</div>

0 commit comments

Comments
 (0)