/*
Online Java - IDE, Code Editor, Compiler

Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/

// An area to improve is to limit the number of divisors, hence fewer bits to represent

import java.util.Scanner;
import java.util.Arrays;

public class Main
{
    public static void main(String[] args) {
        
        int j=0;
        
        int binary[] = new int[31];   //This is to define 31 bit array to hold binary value. Maximum value is over 2 billion.
                                      // This is Java's limitation.
        
        int i;   //counter
        String conversion;
        boolean consecutiveOnes=false;
        
        
        Scanner reader = new Scanner(System.in);  // Reading from System.in
        System.out.println("Enter a number no larger than 2,000,000,000: ");
        
        int num = reader.nextInt(); // Scans the next token of the input as an int.
        
        //once finished
        reader.close();
        
        System.out.println("The decimal number is:  " + num);
        
        int length = binary.length;
        //System.out.println("length: "  + length);
        
        System.out.println("*** This program will convert decimal " + num + "  into binary");
        
        //Execute a for loop to check modulus (i.e no remainder)
        
        
        for (i=length-1; i>=0; i--)
        {
            
                int divisor = (int)(Math.pow(2,i));
                //System.out.println("divisor is:" + divisor);
                
                if (num - divisor >=0)
        {  
           
            binary[(length-1)-i] = 1;
            num=num-divisor;
          
        }
                
            }
            
            conversion = Arrays.toString(binary);
    
    //This will check if there are 3 consecutive 1's as per initial problem
    // Need to ensure that least significant bit is not compared to right handside.
    //Also need to ensure most significant bit is not compared to left hand side
    
           for (i=length-1; i>=0; i--)
     {
         if (i==length-1)            //this is to check for 3 consecutive 1's with the MSB
         {
             if (binary[i]==1 && binary[i-1]==1 && binary[i-2]==1 )
             {
                 //System.out.println("Three consecutive 1s found");
                 consecutiveOnes=true;
                 break;
                 
             }
         }
         
         if (i==0)                 //this is to check for 3 consecutive 1's with the LSB
         {
             if (binary[i]==1 && binary[i+1]==1 && binary[i+2]==1 )
             {
                 consecutiveOnes=true;
                 break;
                 
             }
         }
             
         // this is to check all other bits against adjacent bits for 1s
         if (binary[i]==1 && binary[i+1]==1 && binary[i-1]==1 )
         {
             consecutiveOnes=true;
         
         //System.out.println("Three consecutive 1s found");
         break;
         }
        
       //System.out.println("Three consecutive 1s not found");  
         
     }
     
     
     System.out.println("The binary version is:  " + conversion );
     if (consecutiveOnes)
     {
         System.out.println("Three consecutive 1's found");
     }
     else
     {
        System.out.println("Three consecutive 1's NOT found"); 
     }
        
      }    
    
}