1

I was solving a challenge from HackerRank, it's called Flipping Bits From here: https://www.hackerrank.com/challenges/flipping-bits As I see every case that I have tried is correct You first input the number of numbers you want to convert. Then given a number, it converts it to a 32 unsigned bit binary. Then it flips all the bits 0->1 & 1->0 and then it is converted to decimal and printed. Here's my code, I know it's not optimal and it would give me runtime error but i want to start from here, having the code right.

import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int cases = input.nextInt(); // Número de casos
    Long[] dec_nums = new Long[cases]; // Arreglo contenedor de casos
    for (int i = 0; i < cases; i++) {
        dec_nums[i] = input.nextLong(); // Almacenamiento de casos
    }
    String[] bin_nums = new String[cases]; // Arreglo con dec-bin
    for (int i = 0; i < cases; i++) { //Convertir cada decimal a binario
        bin_nums[i] = (String.format("%032d", (Long.parseLong(Long.toBinaryString(dec_nums[i]))))); 
                                    //Rellenar con 0's
    }
    String[] final_bin=new String[cases];
    for(int i=0;i<cases;i++){
        final_bin[i]="";
        for(int j=0;j<bin_nums[i].length();j++){
            if(bin_nums[i].charAt(j)=='0'){
                final_bin[i]+="1";
            }
            if(bin_nums[i].charAt(j)=='1'){
                final_bin[i]+="0";
            }
        }
    }
    long[]final_dec= new long[cases];
    for(int i=0; i<cases;i++){
        final_dec[i]=0;
        for(int j=0; j<32;j++){
            if (final_bin[i].charAt(j)=='1'){
                final_dec[i]=Long.parseLong(final_bin[i], 2);
            }
        }
    }
    //Imprimir binarios
    System.out.println("Binarios:");
    for (int i = 0; i < cases; i++) {
        System.out.println(bin_nums[i]);
    }
    //Imprimir binarios flipped
    System.out.print("Flipped: ");
    System.out.println(" ");
    for(int i=0; i<cases; i++){
        System.out.println(final_bin[i]);
    }

    System.out.println("Decimales");
    System.out.println(" ");
    for(int i=0; i<cases; i++){
        System.out.println(final_dec[i]);
    }
}
}

The problem is where I input 2147483647 It shows:

Exception in thread "main" java.lang.NumberFormatException: For input >string: "1111111111111111111111111111111" at >java.lang.NumberFormatException.forInputString(NumberFormatException.java>:65) at java.lang.Long.parseLong(Long.java:592) at java.lang.Long.parseLong(Long.java:631) at flipping_bits_v3.Solution.main(Solution.java:17)

and I don't know what it could be, is it a special case i haven't handled? Thank you.

3 Answers3

0

I think the line generating the error is:

bin_nums[i] = (String.format("%032d", (Long.parseLong(Long.toBinaryString(dec_nums[i])))));

You are converting dec_nums[i] to a binary string and then trying to parse that as a long. That's probably what's triggering your error.

It looks like you just want to store the string value of your numbers as binaries in bin_nums, so what I think you want is simply:

bin_nums[i] = Long.toBinaryString(dec_nums[i]);

That should get you over the first hurdle. There may be other issues.

dave
  • 11,641
  • 5
  • 47
  • 65
0

I have modified your solution to keep it simple and concise and clean!!! Please have a look:

import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int cases = input.nextInt(); // Número de casos
    /*Long[] dec_nums = new Long[cases]; You don't need this. Read a number and flip it on the go, without storing*/
    long n;
    int bin[] = new int[32];
    for (int i = 0; i < cases; i++) {
        n = input.nextLong(); // Almacenamiento de casos
        for(int j = 0; j < 32; j++){
            bin[j] = 0;
        }
        long num = n;
        int k = 0;
        while(num != 0){
            bin[k++] = (int)num%2;
            num = num/2;
        }
        //Flipping the binary digits now
        k = 0;
        while(k < 32){
            bin[k] = (bin[k] + 1)%2;
            k++;
        }
        long ans = 0;
        long p = 1;
        k = 0;
        while(k < 32){
            ans = ans + (bin[k]*p);
            k++;
            p = p*2;
        }
        System.out.println(ans);
    }

}
}
User_Targaryen
  • 4,125
  • 4
  • 30
  • 51
0
  1. Convert the given string to Base2 that is binary
  2. Then create a array and fill it with Zero before appending it to converted binary form of the number and then invert the number using map and join it
function flippingBits(n: number): number {
    const binaryForm = n.toString(2)
    let bit32 = (new Array(32 - binaryForm.length).fill(0).join('') + binaryForm).split('').map((x) => 1 - Number(x)).join('')
    return (parseInt(bit32, 2))
}
sachin rai
  • 41
  • 2