/*
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...

import java.math.BigInteger;

public class Main
{
    static boolean isPalindrome=false;
    static String number;
    
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
	    //NO PALINDROME
	     //number="134626926431";     //ODD FLAG   -12 digits   FAIL (it reaches single digit remaining early)
         //number="13469686431";     //EVEN FLAG  - 11 digits  FAIL (it reaches single digit remaining early)
	     //number="142734241";   //(EVEN FLAG)    - 10 digits  FAIL (finishes too early)
	     //number="542734245";   //(EVEN FLAG)    - 9 digits  FAIL (finishes too early)
         //number="54243245";   //(ODD FLAG)      - 8 digits  PASS
         //number="5414245";    //(ODD FLAG)      - 7 digits  FAIL (finishes too early)
         //number="124321";      //(ODD FLAG)     - 6 digits  PASS
         //number="67896";       //(EVEN FLAG)    - 5 digits  PASS
         //number="1231";       //(EVEN FLAG)     - 4 digits  PASS
         //number="124";       //(EVEN FLAG)      - 3 digits  PASS
         //number="63";        //ODD FLAG         - 2 digits  PASS 
        
        //PALINDROME
        //NOW ALL PASSING
        //PALINDROME - EXPLORING BEYOND 5 DIGITS WIDE
        //number="134626626431";     //ODD FLAG   12 digits -  FAIL (it reaches single digit remaining early)
        //number="13462626431";     //EVEN FLAG   11 digits - FAIL (it reaches single digit remaining early)
        //number=1346226431;        //EVEN FLAG   10 digits - FAIL (it reaches single digit remaining early)
         //number="542343245";      //EVEN FLAG   9 digits -FAIL  // (it reaches single digit remaining early)
         number="54244245";         //ODD FLAG    8 digits - PASS
         //number="2349432";        //EVEN FLAG    7 digits  FAIL (it reaches single digit remaining early)
          //number="2341432";       //EVEN FLAG    6 digits FAIL (it reaches single digit remaining early)
         //number="56165";          //EVEN FLAG    5 digits  PASS
         //number="1331";           //ODD FLAG     4 digits   PASS 
         //number="888";            //EVEN FLAG    3 digits  PASS
         //number="121";            // EVEN FLAG   3 digits  PASS
         //number="44";             //ODD FLAG     2 digits  PASS
         //number="9";              //EVEN FLAG    1 digit   PASS
        
        BigInteger pal = new BigInteger(number);
        
        System.out.println("\nThis is initial number: " + number);
        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 temp;
        BigInteger backupTemp = new BigInteger((pal.toString()));
        temp=pal;
        BigInteger firstDigit;
        
        int originalNumberMovesRequired=0;
        //int requiredPalindromeChecks;
        int k=0;
        //int lastDigit;
        int divideBy10Required=0;
        //int temp;
        //int backupTemp; 
        boolean evenFlag=false;
        boolean oddFlag=false;
        boolean adjustedI=false;
        
        //int firstDigit=0;
        int movePosition=0;
        int numberPalindromeChecks=0;
        int m;
        temp=pal;
        
        System.out.println("This is backupTemp: " + backupTemp);
        
        for (m=0; m<backupTemp.toString().length(); m++)
        {
             if(pal.toString().substring(0,pal.toString().length()-1).isEmpty())
            {
                pal = new BigInteger(String.valueOf(temp));
                break;
            }
            else
            {
                divideBy10Required++;
                 pal = new BigInteger(pal.toString().substring(0,pal.toString().length()-1));
            }
        }
        if (m==0)
        {
            isPalindrome=true;
        }
        
        if (divideBy10Required%2==0)
        {
            evenFlag=true;
            System.out.println("EVEN FLAG");
        }
        
        if (divideBy10Required%2!=0)
        {
            System.out.println("ODD FLAG");
            
            oddFlag=true;
        }
        
        for (int i=0; i<divideBy10Required; i++)
        {
            //even flag is for number such as // number=888.
            //it has an odd number of digits.
            //it has got even flag because divideBy10Required(2) is divisble by 2 in order to expose the firstDigit
            //for this number, we have divideBy10Required=1 since we will be left with central number
            //it would have completed numberPalindromeChecks=1 at end of the execution
            //we can see that if we kept the loop like the previous code version:
            //                  1                          1                        TRUE 
            //if ((numberPalindromeChecks)>=(int)(divideBy10Required/2) && numberPalindromeChecks!=0)
            //There are absolutely no issues
            
            //even flag is for number such as // number=56165
            //it has an odd number of digits.
            //it has got even flag because divideBy10Required(4) is divisble by 2 in order to expose the firstDigit
            //for this number, we have divideBy10Required=1 since we will be left with central number
            //it would have completed numberPalindromeChecks=1
            //we can see that if we kept the loop like the previous code version:
            //                1                              2                       TRUE        =FALSE
            //if ((numberPalindromeChecks)>=(int)(divideBy10Required/2) && numberPalindromeChecks!=0)
            //There are issues and it will not validate a single digit.
            //This is reason I introduced the new if loop
            //                1 + 1  =   2                   2                        TRUE       =TRUE
             //if ((numberPalindromeChecks+1)>=(int)(divideBy10Required/2) && numberPalindromeChecks!=0)
            
            if (evenFlag)
            {
                System.out.println("-----number checks complete: " + numberPalindromeChecks);
                //requiredPalindromeChecks = numberPalindromeChecks;
                
                System.out.println("-----divide by 10: " + divideBy10Required/2);
                System.out.println("-----required checks******************************: " + (divideBy10Required/2));
                System.out.println("Original move positions: " + originalNumberMovesRequired);
                // I have had to create this situation due to issues occuring for since it finished palindrome check too early
                //number=134625431;     
                //number=542343245;    
                //number=54244245;     
                //number=2349432;      
                //number=2341432;
                
                /*
                if (hasAdjust)
                    {
                        System.out.println("Single digit remaining: " + ((temp/10)%10));
                    }
                    else
                    {
                        System.out.println("Single digit remaining: " + (temp%10));
                    }
                */
                
                if (numberPalindromeChecks>=(divideBy10Required/2) &&numberPalindromeChecks!=0)
                {
                    System.out.println(temp);
                    
                    //In my old code I had following.. We know once it has reached this point
                    //it is guaranteed palindrome
                    //System.out.println("Single digit remaining: " + (temp%10));
                    //With a number such as 56165, temp would be 5616 at this point.
                    //Performing temp%10 woud give 5(last digit). This is clearly wrong
                    //We are looking to remove last digit  561(6) by performing 561/10  
                    //And then present 56(1) obtained
                    //via 561%10  (the remainder is 1   => 561 - (56x10))
                    
                    //I have had to change this in the biginteger version
                    //BEFORE //System.out.println("Single digit remaining2: " + (temp.toString().substring(0,temp.toString().length()-1)));
                    System.out.println("Single digit remaining2: " + (temp.toString().substring((temp.toString().length()-1),temp.toString().length())));
                    
                    isPalindrome=true;
                    break;
                }
                
            }
            lastDigit=new BigInteger(temp.toString().substring(temp.toString().length()-1));
            
            /*
            //unsure if applicable
            if(lastDigit.toString().substring(0,lastDigit.toString().length()-1).isEmpty())
            {
                lastDigit=new BigInteger(pal.toString());
                //lastDigit=number;
            }
            */
            System.out.println("Current number: " + temp);
            backupTemp = new BigInteger(String.valueOf(temp));
            System.out.println("backup: " + backupTemp);
            boolean hasAdjust = false;
            
            if (evenFlag)
            {
                System.out.println("MOVE POS: " + movePosition);
                System.out.println("**************DECISION: " + movePosition + "val k: " + k);
                System.out.println(movePosition+"-----------------------------------------");
                if (movePosition>=8)
                {
                    System.out.println("1Move position adjusted");
                    movePosition = divideBy10Required-2;
                    hasAdjust=true;
                }
                
                if (movePosition>=6 &&!hasAdjust)
                {
                     System.out.println("2Move position adjusted");
                    movePosition = (divideBy10Required/2)+1;
                    hasAdjust=true;
                }
                
                if(movePosition<6 && !hasAdjust)
                {
                    movePosition = (divideBy10Required/2);
                    System.out.println("3Move position adjusted to: " + movePosition);
                }
            }
            else
            {
                movePosition = movePosition - 2;
            }
            
            if (i==0)
            {
                movePosition = divideBy10Required;
            }
            System.out.println("backup: " + backupTemp);
            divideBy10Required=movePosition;
            System.out.println("Number moves required:" + movePosition);
            originalNumberMovesRequired=movePosition;
            
            //also made the variable declaration higher
            for (k=0; k<movePosition; k++)
            {
                if (k==0)
                {
                    System.out.println("NUMBER DIVISION BY 10 REQUIRED TO EXPOSE FIRST DIGIT: " +  (movePosition));
                }
                
                 temp = new BigInteger(temp.toString().substring(0,temp.toString().length()-1));
                System.out.println("NEW NUMBER (TO EXPOSE FIRST DIGIT ON RIGHT HANDSIDE): " + temp);
            }
            System.out.println("backup: " + backupTemp);
            firstDigit=temp;
            
            if (i>0)
            {
                if(!temp.toString().substring(0,temp.toString().length()-1).isEmpty())
                {
                    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);
            System.out.println("backup: " + backupTemp);
            
            
            /*
            if(firstDigit.toString().substring(0,firstDigit.toString().length()-1).isEmpty())
            {
                System.out.println("Should not reach here");
                
                firstDigit=temp;
                //break;
            }
            */
            
            if (firstDigit.equals(lastDigit))
            {
                System.out.println("backup: " + backupTemp);
                System.out.println("PALINDROME: " + firstDigit + "  " + lastDigit);
                isPalindrome=true;
            }
            else
            {
                isPalindrome=false;
                System.out.println("NOT PALINDROME");
                break;
            }
            
            System.out.println("value i: " + i);
            System.out.println("divideby10:" + divideBy10Required);
            System.out.println("movePosition:"  + movePosition);
            System.out.println("backupTemp:" + backupTemp);
            temp=backupTemp;
            System.out.println("TEMP: " + temp);
            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("k: " + k);
            System.out.println("moveposition:" + k);
            System.out.println("i:" + i);
            System.out.println("divideBy10Required:" + divideBy10Required);
            System.out.println("backup: " + backupTemp);
            
            //in situation such as 54253245 it performs
            //(5)424324(5)
            //5(4)2432(4)5
            //54(2)43(2)45
            //We can see the variables reach this state before it makes conclusion:
            //New number going forward: 54243
            //k: 3
            //moveposition:3
            //i:2
            //divideBy10Required:3
            //And deduces it is a palindrome
            
            //We know k and movePosition operate in for loop
            //and condition fulfilled
            //       3            3      
            //for (k=0; k<movePosition; k++)
            
            //We know that i and divideBy10Required operate in for loop
            //and condition is fulfilled
            //         2              3 
            //for (int i=0; i<divideBy10Required; i++)
            
            //I have devised new logic as below
            //I have had to do it for evenFlag only since with numbers such as 44
            //it will force a second check once established 4 and 4 are palindrome.
            //and it will become invalidated
            
            adjustedI=false;
            
            if (k!=1)
            {
            if (k==movePosition && k==divideBy10Required && ((i+1==k)||((i+2)==k)) && evenFlag)
            {
                System.out.println("00000000000000000000000000000000");
                i--;
                i--;
                adjustedI=true;
            }
            
             if (k==movePosition && k==divideBy10Required && (i==k) && oddFlag)
            {
                System.out.println("8888880000000000000000000000000000000011111");
                i--;
                i--;
                adjustedI=true;
            }
            
            if (k==movePosition && k==divideBy10Required && (i==k) && evenFlag)
            {
                System.out.println("0000000000000000000000000000000011111");
                i--;
                i--;
                adjustedI=true;
            }
            
            if (k==movePosition && k==divideBy10Required && ((i+1)==k) && oddFlag)
            {
                System.out.println("99999999999999999999999");
                i--;
                i--;
                adjustedI=true;
                System.out.println("backup: " + backupTemp);
            }
            }
            
            
            /*
            if (movePosition==3)
            {
                if (movePosition==i)
                {
                    System.out.println("Reduced i counter by 2");
                    i--;
                    i--;
                    System.out.println("NEW i: " + i);
                }
                else
                {
                    System.out.println("Reduced i counter by 1");
                    i--;
                    System.out.println("NEW i: " + i);
                }
            }
            */
            
            
            numberPalindromeChecks++;
        }  //end for
    }
}