Skip to content

Commit 386ca02

Browse files
authored
Merge pull request #344 from sir-gon/feature/flipping_bits
[Hacker Rank] Interview Preparation Kit: Miscellaneous: Flipping bits…
2 parents 5de0964 + 2ee2dad commit 386ca02

File tree

4 files changed

+264
-0
lines changed

4 files changed

+264
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package ae.hackerrank.interview_preparation_kit.miscellaneous;
2+
3+
/**
4+
* AngryFlorist.
5+
*
6+
* @link Problem definition
7+
* [[docs/hackerrank/interview_preparation_kit/miscellaneous/flipping-bits.md]]
8+
*/
9+
public class FlippingBits {
10+
11+
private FlippingBits() {}
12+
13+
/**
14+
* flippingBits.
15+
*/
16+
public static long flippingBits(long n) {
17+
String binaryString = Long.toBinaryString(n);
18+
binaryString = String.format("%32s", binaryString); // Ensure 32 bits
19+
20+
StringBuilder resultBinaryString = new StringBuilder();
21+
22+
for (int i = 0; i < binaryString.length(); i++) {
23+
char binDigit = binaryString.charAt(i);
24+
25+
if (binDigit == '1') {
26+
resultBinaryString.append('0');
27+
} else {
28+
resultBinaryString.append('1');
29+
}
30+
}
31+
32+
return Long.parseLong(resultBinaryString.toString(), 2);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package ae.hackerrank.interview_preparation_kit.miscellaneous;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.IOException;
6+
import java.util.List;
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.TestInstance;
10+
import org.junit.jupiter.api.TestInstance.Lifecycle;
11+
import util.JsonLoader;
12+
13+
/**
14+
* FlippingBitsTflippingBits.
15+
*/
16+
@TestInstance(Lifecycle.PER_CLASS)
17+
class FlippingBitsTest {
18+
/**
19+
* FlippingBitsTestCaseTest.
20+
*/
21+
public static class FlippingBitsTestCaseTest {
22+
public long input;
23+
public long answer;
24+
}
25+
26+
/**
27+
* FlippingBitsTestCase.
28+
*/
29+
public static class FlippingBitsTestCase {
30+
public String title;
31+
public List<FlippingBitsTestCaseTest> tests;
32+
}
33+
34+
private List<FlippingBitsTestCase> testCases;
35+
36+
@BeforeAll
37+
void setup() throws IOException {
38+
String path = String.join("/",
39+
"hackerrank",
40+
"interview_preparation_kit",
41+
"miscellaneous",
42+
"flipping_bits.testcases.json");
43+
44+
this.testCases = JsonLoader.loadJson(path, FlippingBitsTestCase.class);
45+
}
46+
47+
@Test
48+
void testLuckBalance() {
49+
for (FlippingBitsTestCase tests : testCases) {
50+
for (FlippingBitsTestCaseTest test : tests.tests) {
51+
Long result = FlippingBits.flippingBits(test.input);
52+
53+
assertEquals(test.answer, result,
54+
"%s(%s) => must be: %d".formatted(
55+
"FlippingBits.flippingBits",
56+
test.input,
57+
test.answer));
58+
}
59+
}
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
[
2+
{
3+
"title": "Sample Test Case 0",
4+
"tests": [
5+
{
6+
"input": 2147483647,
7+
"answer": 2147483648
8+
},
9+
{
10+
"input": 1,
11+
"answer": 4294967294
12+
},
13+
{
14+
"input": 0,
15+
"answer": 4294967295
16+
}
17+
]
18+
},
19+
{
20+
"title": "Sample Test Case 1",
21+
"tests": [
22+
{
23+
"input": 4,
24+
"answer": 4294967291
25+
},
26+
{
27+
"input": 123456,
28+
"answer": 4294843839
29+
}
30+
]
31+
},
32+
{
33+
"title": "Sample Test Case 2",
34+
"tests": [
35+
{
36+
"input": 0,
37+
"answer": 4294967295
38+
},
39+
{
40+
"input": 802743475,
41+
"answer": 3492223820
42+
},
43+
{
44+
"input": 35601423,
45+
"answer": 4259365872
46+
}
47+
]
48+
}
49+
]
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# [Miscellaneous: Flipping bits](https://www.hackerrank.com/challenges/flipping-bits)
2+
3+
- Difficulty: `#easy`
4+
- Category: `#ProblemSolvingBasic` `#BitManipulation`
5+
6+
You will be given a list of 32 bit unsigned integers.
7+
Flip all the bits ( `1 -> 0` and `0 -> 1`)
8+
and return the result as an unsigned integer.
9+
10+
## Example
11+
12+
$ n = 9_{10} $. We're working with 32 bits, so:
13+
14+
$ 9_{10} = 1001_{2} $
15+
16+
$ 00000000000000000000000000001001_{2} = 9_{10} $
17+
$ 11111111111111111111111111110110_{2} = 4294967286_{10} $
18+
19+
Return `4294967286`
20+
21+
## Function Description
22+
23+
Complete the flippingBits function in the editor below.
24+
25+
flippingBits has the following parameter(s):
26+
27+
- `int n`: an integer
28+
29+
## Returns
30+
31+
- `int`: the unsigned decimal integer result
32+
33+
## Input Format
34+
35+
The first line of the input contains `q`, the number of queries.
36+
Each of the next `q` lines contain an integer, `n`, to process.
37+
38+
## Constraints
39+
40+
- $ 1 \leq q \leq 100 $
41+
- $ 0 \leq n \leq 2^{32} $
42+
43+
## Sample Input 0
44+
45+
```text
46+
3
47+
2147483647
48+
1
49+
0
50+
```
51+
52+
## Sample Output 0
53+
54+
```text
55+
2147483648
56+
4294967294
57+
4294967295
58+
```
59+
60+
## Explanation 0
61+
62+
$ 01111111111111111111111111111111_{2} = 2147483647_{10} $
63+
$ 10000000000000000000000000000000_{2} = 2147483648_{10} $
64+
65+
$ 00000000000000000000000000000001_{2} = 1_{10} $
66+
$ 11111111111111111111111111111110_{2} = 4294967294_{10} $
67+
68+
$ 00000000000000000000000000000000_{2} = 0_{10} $
69+
$ 11111111111111111111111111111110_{2} = 4294967295_{10} $
70+
71+
## Sample Input 1
72+
73+
```text
74+
2
75+
4
76+
123456
77+
```
78+
79+
## Sample Output 1
80+
81+
```text
82+
4294967291
83+
4294843839
84+
```
85+
86+
## Explanation 1
87+
88+
$ 00000000000000000000000000000100_{2} = 4_{10} $
89+
$ 11111111111111111111111111111011_{2} = 4294967291_{10} $
90+
91+
$ 00000000000000011110001001000000_{2} = 4_{10} $
92+
$ 11111111111111100001110110111111_{2} = 429484389_{10} $
93+
94+
## Sample Input 2
95+
96+
```text
97+
3
98+
0
99+
802743475
100+
35601423
101+
```
102+
103+
## Sample Output 2
104+
105+
```text
106+
4294967295
107+
3492223820
108+
4259365872
109+
```
110+
111+
## Explanation 2
112+
113+
$ 00000000000000000000000000000000_{2} = 4_{10} $
114+
$ 11111111111111111111111111111111_{2} = 4294967295_{10} $
115+
116+
$ 00101111110110001110010010110011_{2} = 802743475_{10} $
117+
$ 11010000001001110001101101001100_{2} = 3492223820_{10} $
118+
119+
$ 00000010000111110011110000001111_{2} = 35601423_{10} $
120+
$ 11111101111000001100001111110000_{2} = 4259365872_{10} $

0 commit comments

Comments
 (0)