From 2ee2dad37b0d2ed34e6a1409b9bd008baee1f4e9 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Wed, 9 Jul 2025 00:03:35 -0400 Subject: [PATCH] =?UTF-8?q?[Hacker=20Rank]=20Interview=20Preparation=20Kit?= =?UTF-8?q?:=20Miscellaneous:=20Flipping=20bits.=20Solved=20=E2=9C=93.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../miscellaneous/FlippingBits.java | 34 +++++ .../miscellaneous/FlippingBitsTest.java | 61 +++++++++ .../flipping_bits.testcases.json | 49 +++++++ .../miscellaneous/flipping-bits.md | 120 ++++++++++++++++++ 4 files changed, 264 insertions(+) create mode 100644 algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/miscellaneous/FlippingBits.java create mode 100644 algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/miscellaneous/FlippingBitsTest.java create mode 100644 algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/miscellaneous/flipping_bits.testcases.json create mode 100644 docs/hackerrank/interview_preparation_kit/miscellaneous/flipping-bits.md diff --git a/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/miscellaneous/FlippingBits.java b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/miscellaneous/FlippingBits.java new file mode 100644 index 0000000..9794f68 --- /dev/null +++ b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/miscellaneous/FlippingBits.java @@ -0,0 +1,34 @@ +package ae.hackerrank.interview_preparation_kit.miscellaneous; + +/** + * AngryFlorist. + * + * @link Problem definition + * [[docs/hackerrank/interview_preparation_kit/miscellaneous/flipping-bits.md]] + */ +public class FlippingBits { + + private FlippingBits() {} + + /** + * flippingBits. + */ + public static long flippingBits(long n) { + String binaryString = Long.toBinaryString(n); + binaryString = String.format("%32s", binaryString); // Ensure 32 bits + + StringBuilder resultBinaryString = new StringBuilder(); + + for (int i = 0; i < binaryString.length(); i++) { + char binDigit = binaryString.charAt(i); + + if (binDigit == '1') { + resultBinaryString.append('0'); + } else { + resultBinaryString.append('1'); + } + } + + return Long.parseLong(resultBinaryString.toString(), 2); + } +} diff --git a/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/miscellaneous/FlippingBitsTest.java b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/miscellaneous/FlippingBitsTest.java new file mode 100644 index 0000000..6916f71 --- /dev/null +++ b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/miscellaneous/FlippingBitsTest.java @@ -0,0 +1,61 @@ +package ae.hackerrank.interview_preparation_kit.miscellaneous; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.util.List; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; +import util.JsonLoader; + +/** + * FlippingBitsTflippingBits. + */ +@TestInstance(Lifecycle.PER_CLASS) +class FlippingBitsTest { + /** + * FlippingBitsTestCaseTest. + */ + public static class FlippingBitsTestCaseTest { + public long input; + public long answer; + } + + /** + * FlippingBitsTestCase. + */ + public static class FlippingBitsTestCase { + public String title; + public List tests; + } + + private List testCases; + + @BeforeAll + void setup() throws IOException { + String path = String.join("/", + "hackerrank", + "interview_preparation_kit", + "miscellaneous", + "flipping_bits.testcases.json"); + + this.testCases = JsonLoader.loadJson(path, FlippingBitsTestCase.class); + } + + @Test + void testLuckBalance() { + for (FlippingBitsTestCase tests : testCases) { + for (FlippingBitsTestCaseTest test : tests.tests) { + Long result = FlippingBits.flippingBits(test.input); + + assertEquals(test.answer, result, + "%s(%s) => must be: %d".formatted( + "FlippingBits.flippingBits", + test.input, + test.answer)); + } + } + } +} diff --git a/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/miscellaneous/flipping_bits.testcases.json b/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/miscellaneous/flipping_bits.testcases.json new file mode 100644 index 0000000..2e5d736 --- /dev/null +++ b/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/miscellaneous/flipping_bits.testcases.json @@ -0,0 +1,49 @@ +[ + { + "title": "Sample Test Case 0", + "tests": [ + { + "input": 2147483647, + "answer": 2147483648 + }, + { + "input": 1, + "answer": 4294967294 + }, + { + "input": 0, + "answer": 4294967295 + } + ] + }, + { + "title": "Sample Test Case 1", + "tests": [ + { + "input": 4, + "answer": 4294967291 + }, + { + "input": 123456, + "answer": 4294843839 + } + ] + }, + { + "title": "Sample Test Case 2", + "tests": [ + { + "input": 0, + "answer": 4294967295 + }, + { + "input": 802743475, + "answer": 3492223820 + }, + { + "input": 35601423, + "answer": 4259365872 + } + ] + } +] diff --git a/docs/hackerrank/interview_preparation_kit/miscellaneous/flipping-bits.md b/docs/hackerrank/interview_preparation_kit/miscellaneous/flipping-bits.md new file mode 100644 index 0000000..5544b33 --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/miscellaneous/flipping-bits.md @@ -0,0 +1,120 @@ +# [Miscellaneous: Flipping bits](https://www.hackerrank.com/challenges/flipping-bits) + +- Difficulty: `#easy` +- Category: `#ProblemSolvingBasic` `#BitManipulation` + +You will be given a list of 32 bit unsigned integers. +Flip all the bits ( `1 -> 0` and `0 -> 1`) +and return the result as an unsigned integer. + +## Example + +$ n = 9_{10} $. We're working with 32 bits, so: + +$ 9_{10} = 1001_{2} $ + +$ 00000000000000000000000000001001_{2} = 9_{10} $ +$ 11111111111111111111111111110110_{2} = 4294967286_{10} $ + +Return `4294967286` + +## Function Description + +Complete the flippingBits function in the editor below. + +flippingBits has the following parameter(s): + +- `int n`: an integer + +## Returns + +- `int`: the unsigned decimal integer result + +## Input Format + +The first line of the input contains `q`, the number of queries. +Each of the next `q` lines contain an integer, `n`, to process. + +## Constraints + +- $ 1 \leq q \leq 100 $ +- $ 0 \leq n \leq 2^{32} $ + +## Sample Input 0 + +```text +3 +2147483647 +1 +0 +``` + +## Sample Output 0 + +```text +2147483648 +4294967294 +4294967295 +``` + +## Explanation 0 + +$ 01111111111111111111111111111111_{2} = 2147483647_{10} $ +$ 10000000000000000000000000000000_{2} = 2147483648_{10} $ + +$ 00000000000000000000000000000001_{2} = 1_{10} $ +$ 11111111111111111111111111111110_{2} = 4294967294_{10} $ + +$ 00000000000000000000000000000000_{2} = 0_{10} $ +$ 11111111111111111111111111111110_{2} = 4294967295_{10} $ + +## Sample Input 1 + +```text +2 +4 +123456 +``` + +## Sample Output 1 + +```text +4294967291 +4294843839 +``` + +## Explanation 1 + +$ 00000000000000000000000000000100_{2} = 4_{10} $ +$ 11111111111111111111111111111011_{2} = 4294967291_{10} $ + +$ 00000000000000011110001001000000_{2} = 4_{10} $ +$ 11111111111111100001110110111111_{2} = 429484389_{10} $ + +## Sample Input 2 + +```text +3 +0 +802743475 +35601423 +``` + +## Sample Output 2 + +```text +4294967295 +3492223820 +4259365872 +``` + +## Explanation 2 + +$ 00000000000000000000000000000000_{2} = 4_{10} $ +$ 11111111111111111111111111111111_{2} = 4294967295_{10} $ + +$ 00101111110110001110010010110011_{2} = 802743475_{10} $ +$ 11010000001001110001101101001100_{2} = 3492223820_{10} $ + +$ 00000010000111110011110000001111_{2} = 35601423_{10} $ +$ 11111101111000001100001111110000_{2} = 4259365872_{10} $ pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy