Tuesday, 23 December 2014

Hackerrank Flipping Bits Solution


Problem Statement
You will be given a list of 32-bits unsigned integers. You are required to output the list of the unsigned integers you get by flipping bits in its binary representation (i.e. unset bits must be set, and set bits must be unset).
Input Format
The first line of the input contains the list size T. T lines follow each line having an integer from the list.
Output Format
Output one line per element from the list with the requested result.
Sample Input
3 
2147483647 
1 
0
Sample Output
2147483648 
4294967294 
4294967295
Explanation
Take 1 for example, as unsigned 32-bits is 00000000000000000000000000000001 and doing the flipping we get 11111111111111111111111111111110 which in turn is 4294967294

Source Code:

 import java.io.*;  
 import java.util.*;  
 import java.text.*;  
 import java.math.*;  
 import java.util.regex.*;  
 public class Solution {  
   public static void main(String[] args) {  
     /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */  
     Scanner sc = new Scanner(System.in);  
     int t;  
     t = sc.nextInt();  
     while((t--)>0){  
       Long x = sc.nextLong();  
       String ans="",anss="";  
       String s = Long.toBinaryString(x);  
       for(int i = 0,j = s.length()-1;i<32;i++){  
         if(j>=0 && s.charAt(j) =='1'){  
           ans = ans + "0";  
         }  
         else  
           {  
           ans = ans + "1";  
         }  
         {  
           j--;  
         }  
       }  
       for(int i = 31;i>=0;i--){  
         anss = anss+ans.charAt(i);  
       }  
       String bin = anss;  
     long number = 0;  
     int dig;  
     for (int i = 0; i < bin.length(); i++){  
       dig = bin.charAt(i) - '0';  
       number = 2 * number + dig;  
     }  
       System.out.println( Long.toString(number));  
     }  
   }  
 }  

** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)

16 comments:

  1. can u explain this loop?

    for(int i = 0,j = s.length()-1;i<32;i++){
    if(j>=0 && s.charAt(j) =='1'){
    ans = ans + "0";
    }
    else
    {
    ans = ans + "1";
    }
    {
    j--;
    }
    }

    ReplyDelete
    Replies
    1. for(int i = 0,j = s.length()-1;i<32;i++)//loop runs for 32 times(i=0 to i = 31)
      {
      if(j>=0 && s.charAt(j) =='1')/*(j>=0 - just to make sure the loop runs just 32 times and not more than that, )
      s.char(j)=='1' - checks whether the character is 1 */
      {
      ans = ans + "0"; // if it is 1 add 0 to the answer string
      }
      else
      {
      ans = ans + "1"; // if it is 0 add 1 to the answer string
      }
      {
      j--; // decrements from 31 to 0 (total 32 bits)
      }
      }


      Thanks for visiting the blog.
      I hope this clarifies your doubt.

      Please comment below if you have any further doubts :)

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;

    public class Solution {

    public static long flip(String val)
    {
    long finalValue = 0;
    for(int i=0; i<val.length();i++)
    {
    if(val.charAt(i) != '1')
    finalValue = finalValue*2 + 1;
    else
    finalValue *=2;
    }
    return finalValue;
    }
    static String convertToString(String s)
    {
    if(s.length()!=32)
    {
    int i = 32 - s.length();
    while(i!=0)
    {
    s = "0"+s;
    i--;
    }
    }
    return s;
    }
    public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    try{
    int lines = Integer.parseInt(in.readLine());
    for(int i=0; i< lines; i++)
    {
    //String s = convertToString(Long.parseLong(in.readLine()));
    String s = Long.toBinaryString(Long.parseLong(in.readLine()));
    s = convertToString(s);
    long val = flip(s);
    System.out.println(String.valueOf(val));
    }
    }
    catch(Exception e)
    {

    }
    }
    }

    ReplyDelete
  4. In C:

    #include
    #include
    #include
    unsigned int reverseBits(unsigned int num)
    {
    return ~num;
    }
    int main() {
    int n=0,i;
    scanf("%d",&n);
    unsigned int a[n],b[n];
    for(i=0;i<n;i++){
    scanf("%u",&a[i]);
    }
    for(i=0;i<n;i++){
    printf("%u\n",reverseBits(a[i]));
    }
    }

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete

  6. import java.io.IOException;
    import java.util.Scanner;

    public class FlippingBits {
    static int t;
    static long[] results;

    public static void main(String[] args) throws NumberFormatException,
    IOException {
    Scanner in = new Scanner(System.in);
    t = in.nextInt();
    results = new long[t];
    for (int i = 0; i < t; i++) {
    long n = in.nextLong();
    results[i] = flipBits(n);
    }
    for (int i = 0; i < t; i++) {
    System.out.println(results[i]);
    }
    in.close();
    }//main

    private static long flipBits(long n) {
    long mask = 0xffffffffl;
    long result = n ^ mask;
    return result;
    }//flipbits
    }//class

    ReplyDelete
  7. can anyone please suggest me where i have done wrong nd what modifications i need to do

    import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;
    public class Solution {
    public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    Scanner sc=new Scanner(System.in);
    int T=sc.nextInt();
    int r[]=new int[32];
    if((1<=T)&&(T<=100))
    {
    for(int i=0;i<T;i++)
    {
    int x=sc.nextInt();
    double s=0;
    if((x==1)||(x==0))
    {

    for(int j=0;j<32;j++)
    {

    r[j]=x%2;
    x=x/2;
    }
    for(int j=0;j<32;j++)
    {
    if(r[j]==0)
    {
    r[j]=1;
    }
    else{
    r[j]=0;
    }
    }

    for(int j=0;j<32;j++)
    {
    s=s+(r[j]*(Math.pow(2,j)));

    }


    System.out.println(s);

    }
    else if(x!=1)
    {
    for(int j=0;j<32;j++)
    {

    r[j]=x%2;
    x=x/2;


    }
    for(int j=0;j<32;j++)
    {


    if(r[j]==0)
    {
    r[j]=1;
    }
    else{
    r[j]=0;
    }

    }

    for(int j=0;j<32;j++)
    {

    s=s+(r[j]*(Math.pow(2,j)));
    }

    System.out.println(s);

    }

    }
    }

    }
    } Your Output (stdout) 2.147483648E9 4.294967294E9 4.294967295E9 Expected Output 2147483648 4294967294 4294967295

    ReplyDelete
  8. it is satisfying for few test cases....what data type should i assign to x ??


    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    import java.math.BigInteger.*;

    public class Solution {

    public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    Scanner sc=new Scanner(System.in);
    int T=sc.nextInt();
    int r[]=new int[33];
    int j;


    for(int i=0;i<T;i++)
    {
    int x=sc.nextInt();
    long s=0;
    if((x==1)||(x==0))
    {
    j=0;
    while(j<32)
    {
    r[j]=x%2;

    x=x/2;
    j++;
    }
    for(j=0;j<32;j++)
    {
    if(r[j]==0)
    {
    r[j]=1;

    }
    else{
    r[j]=0;

    }
    }

    for(j=0;j<32;j++)
    {
    s+=(r[j]*(Math.pow(2,j)));



    }


    System.out.println(s);

    }
    else if(x!=1||x!=0)
    {
    j=0;
    while(j<32)
    {

    r[j]=x%2;
    x=x/2;
    j++;

    }
    for( j=0;j<32;j++)
    {


    if(r[j]==0)
    {
    r[j]=1;
    }
    else{
    r[j]=0;
    }

    }

    for( j=0;j<32;j++)
    {

    s+=(r[j]*(Math.pow(2,j)));
    }

    System.out.println(s);

    }

    }
    }
    }

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. static long solveMe(long a) {
    String bitsString = Long.toBinaryString(~a).substring(32);
    long b = Long.parseLong(bitsString, 2);
    return b;
    }

    ReplyDelete
  11. #include
    #include
    int reverseBits(int num)
    {
    return ~num;
    }
    int main()
    {
    int n=0,i;
    scanf("%d",&n);
    unsigned int a[n],b[n];
    for(i=0;i<n;i++)
    {
    scanf("%u",&a[i]);
    }
    for(i=0;i<n;i++)
    {
    printf("%u\n",reverseBits(a[i]));
    }
    }

    ReplyDelete