/*
Online Java - IDE, Code Editor, Compiler

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

//as per the documentation, the code has to handle situation differently depending
//on the width of the number...

//If this challenge was to incorporate BigInteger, in areas where % is used, there would be no changes
//required to the coding
//In areas where /10 is used, it would have an impact... Since we know that we can not cast BigInteger to a long
//or Integer since the data is potentially too wide...
//Only option is to change the logic into String.. The last digit can be removed via SubString.
//I will use the same techniques as the challenge stated (i.e processing as if the initial Palindrome was an Integer)

import java.math.BigInteger;

public class Main
{
    static boolean isPalindrome=false;
    static int number;
    
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");

	    //NO PALINDROME
        //number=54243245;   // odd number of divideBy10Required
        //number=678;   
        //number=63;
        
        //PALINDROME
        //number=56165;   // even number of divideBy10Required
        //number=121;    // even number of divideBy10Required
        //number=888;
        //number=44;    
        //number=9;
        
        BigInteger div;
        
        //EXPLORING BEYOND 5 DIGITS WIDE
           number = 1346336431;   //NO ISSUES   10 digits  (number less than Java limit 2,147,483,647)
         //number =  542343245;   //ISSUES      9 digits
         //number =   54244245;   //NO ISSUES   8 digits
         //number =    2349432;   //ISSUES      7 digits
         //number =     234432;   //NO ISSUES   6 digits
         //number =      56165;   //NO ISSUES   5 digits
        
        BigInteger pal = new BigInteger(String.valueOf(number));
        
        
        System.out.println("\nThis is initial number: " + pal);
        palindrome(pal);
        
        System.out.println("------------------------------------------");
        System.out.println(number +  " is a palindrome:   " + isPalindrome);
        System.out.println("------------------------------------------");
    }
    
    public static void palindrome(BigInteger pal)
    {
        BigInteger lastDigit;
        BigInteger divideBy10Required = new BigInteger("0");
        BigInteger temp;
        BigInteger backupTemp;
        BigInteger counter;
        BigInteger zero = new BigInteger("0");
        BigInteger one = new BigInteger("1");
        BigInteger two = new BigInteger("2");
        
        boolean evenFlag=false;
        boolean oddFlag=false;
    
        BigInteger firstDigit;
        BigInteger movePosition = new BigInteger("0");
        BigInteger numberPalindromeChecks = new BigInteger("0");
        int m;
        temp=pal;
        
        //this required modification also
        //for (int i=0; i<divideBy10Required; i++)
        
        //These are the values when performing compareTo() on BigIntegers        
        //1: when the first BigDecimal is greater than the second BigDecimal.
        //0: when the first BigDecimal is equal to the second BigDecimal.
        //-1: when the first BigDecimal is less than the second BigDecimal.
        
        //in the expression we are performing an increment to the index
        
        System.out.println(divideBy10Required);   
        //for (m = zero; (m.compareTo(divideBy10Required)==-1); m.add(one))
        for (m=0; m<10; m++)
        {
           System.out.println("INSIDE HERE");
            //counter++;
            
            //need to modify this bit of code
            //if ((int)(number/10)==0)
            if(pal.toString().substring(0,pal.toString().length()-1).isEmpty())
            {
                //this also needs changing 
                //number=temp;
                pal = new BigInteger(String.valueOf(temp));
                
                break;
            }
            else
            {
                //modification required
                //divideBy10Required++;
                divideBy10Required.add(one);
                
                //we would require code change here
                //we are effectively reducing the length by one digit
                //number=number/10;
                System.out.println("REACH");
                pal = new BigInteger(pal.toString().substring(0,pal.toString().length()-1));
                System.out.println("PAL: " + pal);
                System.out.println("DIVIDE BY 10 REQUIRED: " + divideBy10Required);
            }
        }
        
        //modification required
        //m==0
        if (m==0)
        {
            isPalindrome=true;
        }
        
        //modifications required for %
        //if (divideBy10Required%2==0)
        if (divideBy10Required.divide(two).equals(0))
        
        {
            evenFlag=true;
        }
        
        //no modifications required for %
        if (divideBy10Required.divide(two).equals(0))
        {
            oddFlag=true;
        }
        
        //need to modify this
        //for (int i=0; i<divideBy10Required; i++)
        for (BigInteger i = new BigInteger(String.valueOf("0")); (i.compareTo(divideBy10Required)==-1); i.add(one))
        
        {
            //System.out.println("BACK HERE: " + divideBy10Required);
            //System.out.println("BACK HERE: " + i);
            
            if (evenFlag)
            {
                //need to modify this
                //These are the values when performing compareTo() on BigIntegers        
                //1: when the first BigDecimal is greater than the second BigDecimal.
                //0: when the first BigDecimal is equal to the second BigDecimal.
                //-1: when the first BigDecimal is less than the second BigDecimal.
                
                //it becomes tricky with greater than or equal to
                //since there are no returned values that correspond to this in BigInteger 
                //if (numberPalindromeChecks>=(int)(divideBy10Required/2) && numberPalindromeChecks!=0)
                //I am not sure if this can even be handled
                //we would now perform minus 1 on numberPalindromeChecks since we had condition >=
                //we know 1 is returned from compareTo() when using >
                    
                if((numberPalindromeChecks.subtract(one).compareTo(divideBy10Required.divide(two))==1) && !numberPalindromeChecks.equals(0))
                {
                    //this requires modification
                    //System.out.println("Single digit remaining: " + (temp%10));
                    System.out.println("Single digit remaining: " + (temp.toString().substring(0,temp.toString().length()-1)));
                    isPalindrome=true;
                    break;
                }
            }
            //modifications required for %
            //lastDigit=temp%10;
            lastDigit=new BigInteger(temp.toString().substring(temp.toString().length()-1));

            //modifications required for %
            //if ((lastDigit%10)==0)
            if(lastDigit.toString().substring(0,lastDigit.toString().length()-1).isEmpty())
            {
                //We would require modification here...
                //number would need to ber changed to String and then get integer value
                //lastDigit=number;
                //lastDigit = Integer.valueOf(pal.toString());
                lastDigit=pal;
            }
            
            System.out.println("Current number: " + temp);
            //System.out.println("Number divsion by 10: " + divideBy10Required);
            
            //we also need to change these to BigInteger
            //backupTemp=temp;
            backupTemp = new BigInteger(String.valueOf(temp));
            
            boolean hasAdjust = false;
            
            if (evenFlag)
            {
                //need modification here, once again,
                //These are the values when performing compareTo() on BigIntegers        
                //1: when the first BigDecimal is greater than the second BigDecimal.
                //0: when the first BigDecimal is equal to the second BigDecimal.
                //-1: when the first BigDecimal is less than the second BigDecimal.
                //if (movePosition>=8)
                if (movePosition.compareTo(new BigInteger("8"))==0)
                {
                    //need to modify this
                    //movePosition = divideBy10Required-2;
                    BigInteger movePositionRequirement = new BigInteger((String.valueOf(divideBy10Required.subtract(two))));
                    movePosition = movePositionRequirement;
                    
                    hasAdjust=true;
                }
                
                //need to modify this
                //if (movePosition>=6 &&!hasAdjust)
                if (movePosition.subtract(one).compareTo(new BigInteger("6"))==0 && hasAdjust)
                {
                    //movePosition = (divideBy10Required/2)+1;
                    BigInteger movePositionRequirement = new BigInteger((String.valueOf(divideBy10Required.divide(two).add(one))));
                    
                    hasAdjust=true;
                }
                
                //need to modify this
                //need modification here, once again,
                //These are the values when performing compareTo() on BigIntegers        
                //1: when the first BigDecimal is greater than the second BigDecimal.
                //0: when the first BigDecimal is equal to the second BigDecimal.
                //-1: when the first BigDecimal is less than the second BigDecimal.
                
                //if(movePosition<6 && !hasAdjust)
                if (movePosition.compareTo(new BigInteger("6"))==-1 && hasAdjust)
                
                {
                    //modification required
                    //movePosition = (divideBy10Required/2);
                    movePosition = new BigInteger((String.valueOf(divideBy10Required.divide(two))));
                }
            }
            else
            {
                //modification required
                //movePosition = movePosition - 2;
                movePosition = new BigInteger((String.valueOf(movePosition.subtract(two))));
            }
            
            if (i.equals(0))
            {
                //modification required
                movePosition = divideBy10Required;
            }
            
            divideBy10Required=movePosition;
            
            //System.out.println("***********");
            //System.out.println("even flag: " + evenFlag);
            //System.out.println("odd flag:" + oddFlag);
            System.out.println("Number moves required:" + movePosition);
            //System.out.println("***********");
            
            //need to modify this
            //for (int k=0; k<movePosition; k++)
            for (BigInteger k = new BigInteger(String.valueOf("0")); (k.compareTo(divideBy10Required)==-1); k.add(one))
            {
                //need to modify this 
                //if (k==0)
                if (k.equals(0))
                {
                    System.out.println("NUMBER DIVISION BY 10 REQUIRED TO EXPOSE FIRST DIGIT: " +  (movePosition));
                }
                
                //this also requires adjustments
                //temp= (int) (temp/10);
                temp = new BigInteger(temp.toString().substring(0,temp.toString().length()-1));

                System.out.println("NEW NUMBER (TO EXPOSE FIRST DIGIT ON RIGHT HANDSIDE): " + temp);
            }
            
            //modifications required
            //firstDigit=temp;
            firstDigit=temp;
            
            //need to modify this 
            //if (i>0)
            if (i.compareTo(zero)==1)
            {
                //modifications required
                //if (temp%10!=0)
                if(!temp.toString().substring(0,temp.toString().length()-1).isEmpty())
                {
                    //modification required here
                    //firstDigit= temp%10;
                    firstDigit=new BigInteger(temp.toString().substring(temp.toString().length()-1));
                }
            }
            System.out.println("This is first digit: " + firstDigit);
            System.out.println("This is last digit: " + lastDigit);
            
            //modification required here
            //if (firstDigit%10==0)
            if(firstDigit.toString().substring(0,firstDigit.toString().length()-1).isEmpty())
            {
                System.out.println("Should not reach here");
                firstDigit=temp;
            }
            
            if (firstDigit==lastDigit)
            {
                System.out.println("PALINDROME: " + firstDigit + "  " + lastDigit);
                isPalindrome=true;
            }
            else
            {
                isPalindrome=false;
                System.out.println("NOT PALINDROME");
                break;
            }
            
            temp=backupTemp;
            //modification required here
            //temp = (int)(temp/10);
            temp = new BigInteger(temp.toString().substring(0,temp.toString().length()-1));
            
            System.out.println("----------------------");
            System.out.println("New number going forward: " + temp);
            //System.out.println("i: " + i);
            //System.out.println("movePosition: " + movePosition);
            
            //need to modify this
            //if (movePosition==3)
            if (movePosition.equals("3"))
            {
                if (movePosition==i)
                {
                    System.out.println("Reduced i counter by 2");
                    
                    //need to modify this
                    //i--;
                    //i--;
                    i.subtract(one);
                    i.subtract(one);
                }
                else
                {
                    System.out.println("Reduced i counter by 1");
                    
                    //need to modify this
                    //i--;
                    i.subtract(one);
                }
            }
            
            //System.out.println("divideBy10Required: " + divideBy10Required);
            //System.out.println("This is i: " + i);
            
            //need to modify this
            //numberPalindromeChecks++;
            numberPalindromeChecks.add(one);
        }
    }
}