Skip to content

Commit cb84167

Browse files
add 3226
1 parent fac1193 commit cb84167

File tree

3 files changed

+67
-0
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src

3 files changed

+67
-0
lines changed

Diff for: paginated_contents/algorithms/4th_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
3+
| 3226 | [Number of Bit Changes to Make Two Integers Equal](https://leetcode.com/problems/number-of-bit-changes-to-make-two-integers-equal/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3226.java) | | Easy |
34
| 3224 | [Minimum Array Changes to Make Differences Equal](https://leetcode.com/problems/minimum-array-changes-to-make-differences-equal/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3224.java) | | Medium |
45
| 3223 | [Minimum Length of String After Operations](https://leetcode.com/problems/minimum-length-of-string-after-operations/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3223.java) | | Medium |
56
| 3222 | [Find the Winning Player in Coin Game](https://leetcode.com/problems/find-the-winning-player-in-coin-game/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3222.java) | | Easy |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
public class _3226 {
4+
public static class Solution1 {
5+
public int minChanges(int n, int k) {
6+
if (n == k) {
7+
return 0;
8+
}
9+
String nBin = Integer.toBinaryString(n);
10+
String kBin = Integer.toBinaryString(k);
11+
if (nBin.length() > kBin.length()) {
12+
StringBuilder sb = new StringBuilder(kBin);
13+
sb.reverse();
14+
while (nBin.length() > sb.length()) {
15+
sb.append("0");
16+
}
17+
kBin = sb.reverse().toString();
18+
}
19+
if (nBin.length() != kBin.length()) {
20+
return -1;
21+
}
22+
int minChanges = 0;
23+
for (int i = nBin.length() - 1; i >= 0; i--) {
24+
if (nBin.charAt(i) != kBin.charAt(i)) {
25+
if (nBin.charAt(i) == '1') {
26+
minChanges++;
27+
} else {
28+
return -1;
29+
}
30+
}
31+
}
32+
return minChanges;
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import com.fishercoder.solutions.fourththousand._3226;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _3226Test {
10+
private static _3226.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _3226.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(2, solution1.minChanges(13, 4));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(-1, solution1.minChanges(44, 2));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(-1, solution1.minChanges(2, 47));
30+
}
31+
}

0 commit comments

Comments
 (0)