Skip to content

Commit 9520a85

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Miscellaneous: Flipping bits. Solved ✓.
1 parent 5834c4d commit 9520a85

File tree

4 files changed

+255
-0
lines changed

4 files changed

+255
-0
lines changed
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} $
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// @link Problem definition [[docs/hackerrank/interview_preparation_kit/miscellaneous/flipping-bits.md]]
2+
3+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit.miscellaneous;
4+
5+
using System.Diagnostics.CodeAnalysis;
6+
7+
public static class FlippingBits
8+
{
9+
10+
public static long flippingBits(long n)
11+
{
12+
string n_bin_str = Convert.ToString(n, 2);
13+
n_bin_str = n_bin_str.PadLeft(32, '0'); // Ensure 32 bits
14+
string result_bin_str = "";
15+
16+
foreach (char bin_digit in n_bin_str)
17+
{
18+
if (bin_digit == '1')
19+
{
20+
result_bin_str = result_bin_str + '0';
21+
}
22+
else
23+
{
24+
result_bin_str = result_bin_str + '1';
25+
}
26+
}
27+
28+
long number = Convert.ToUInt32(result_bin_str, 2);
29+
30+
return number;
31+
}
32+
}
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+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.miscellaneous;
2+
3+
using algorithm_exercises_csharp_test.common;
4+
using algorithm_exercises_csharp.hackerrank.interview_preparation_kit.miscellaneous;
5+
6+
[TestClass]
7+
public class FlippingBitsTest
8+
{
9+
public class FlippingBitsTestCaseTest(long input, long answer)
10+
{
11+
public long Input { get; } = input;
12+
public long Answer { get; } = answer;
13+
}
14+
15+
public class FlippingBitsTestCase(string title, List<FlippingBitsTestCaseTest> tests)
16+
{
17+
public string Title { get; } = title;
18+
public List<FlippingBitsTestCaseTest> Tests { get; } = tests;
19+
}
20+
21+
private List<FlippingBitsTestCase> testCases { get; set; } = default!;
22+
23+
[TestInitialize]
24+
public void testInitialize()
25+
{
26+
testCases = JsonLoader.resourceLoad<List<FlippingBitsTestCase>>(
27+
"hackerrank/interview_preparation_kit/miscellaneous/flipping_bits.testcases.json"
28+
) ?? [];
29+
}
30+
31+
[TestMethod]
32+
public void testFlippingBits()
33+
{
34+
long result;
35+
36+
foreach (FlippingBitsTestCase tests in testCases)
37+
{
38+
foreach (FlippingBitsTestCaseTest test in tests.Tests)
39+
{
40+
result = FlippingBits.flippingBits(test.Input);
41+
Assert.AreEqual(
42+
test.Answer,
43+
result,
44+
string.Format(
45+
System.Globalization.CultureInfo.InvariantCulture,
46+
"FlippingBits.flippingBits({0}) => must be: {1}",
47+
test.Input,
48+
test.Answer
49+
)
50+
);
51+
}
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)