/*
Online Java - IDE, Code Editor, Compiler

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

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 :)");
        
        //if ((numberPalindromeChecks+1)>=(int)(divideBy10Required/2) && numberPalindromeChecks!=0)
                
        
	      //NO PALINDROME
	      //number="1427347241";   //(EVEN FLAG)    - 10 digits  PASS
	      //number="542734245";   //(EVEN FLAG)    - 9 digits    PASS
          //number="54243245";   //(ODD FLAG)      - 8 digits    PASS
          //number="5414245";    //(ODD FLAG)      - 7 digits    PASS
          //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="1427337241";   //(EVEN FLAG)    - 10 digits  PASS
        //number="542343245";    //EVEN FLAG        9 digits  PASS
        //number="54244245";     //ODD FLAG         8 digits  PASS
        //number="2349432";      //EVEN FLAG        7 digits  PASS
        //number="2341432";  //EVEN FLAG            6 digits  PASS
        //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
        
        //NO PALINDROME  (Exploring 11 digits and beyond - BigInteger)
        
        //modified most central numbers to invalidate palindrome (based on chatGPT sample)
        //number="1234567890987654321234567890987654321234567890687654321234567890987654321234567890987654321";   //(EVEN FLAG)    - 100 digits  PASS
        
        //modified most central numbers to invalidate palindrome (based on chatGPT sample)
        number="12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210987654321";
        
        
        //number="1427365792345123456789630569876543215432975637241";   //(ODD FLAG)    - 50 digits  PASS
        //number="14273657923456075432975637241";   //(ODD FLAG)    - 30 digits  PASS
        //number="1427365790875637241";   //(ODD FLAG)    - 19 digits  PASS
        //number="142736571875637241";   //(ODD FLAG)    - 18 digits  PASS
        //number="14273657185637241";   //(EVEN FLAG)    - 17 digits  PASS
        //number="1427365725637241";   //(ODD FLAG)    - 16 digits  PASS
        //number="142736596637241";   //(ODD FLAG)    - 15 digits  PASS
        //number="14273656637241";   //(ODD FLAG)    - 14 digits  PASS
        //number="1427365837241";   //(ODD FLAG)    - 13 digits  FAIL (finishes too early)
        //number="142730137241";   //(EVEN FLAG)    - 12 digits  FAIL (finishes too early) - FIXED
        //number="14275037241";   //(EVEN FLAG)    - 11 digits  PASS        
        
        //PALINDROME  (Exploring 11 digits and beyond - BigInteger)
        
        //using chatGPT to generate 100 digit number
        //number="1234567890987654321234567890987654321234567890987654321234567890987654321234567890987654321"; //(EVEN FLAG)    - 100 digits  PASS
        
        //using chatGPT to generate 500 digit number
        //number="12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678900987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210987654321";
        
        //number="14273657923456065432975637241";   //(ODD FLAG)    - 30 digits  PASS
        //number="1427365790975637241";   //(ODD FLAG)    - 19 digits  PASS
        //number="142736571175637241";   //(ODD FLAG)    - 18 digits  PASS
        //number="14273657175637241";   //(ODD FLAG)    - 17 digits  PASS
        //number="1427365775637241";   //(ODD FLAG)    - 16 digits  PASS
        //number="142736595637241";   //(ODD FLAG)    - 15 digits  PASS
        //number="14273699637241";   //(ODD FLAG)    - 14 digits  PASS
        //number="1427309037241";   //(ODD FLAG)    - 13 digits  FAIL (finishes too early)
        //number="142730037241";   //(ODD FLAG)    - 12 digits  FAIL (finishes too early)
        //number="14273037241";   //(EVEN FLAG)    - 11 digits  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;
        
        boolean hasAdjust=false;
        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;
        
        //for (m=0; m<10; m++)
        for (m=0; m<backupTemp.toString().length(); m++)
        {
            //if ((int)(number/10)==0)
            if(pal.toString().substring(0,pal.toString().length()-1).isEmpty())
            {
                //number=temp;
                pal = new BigInteger(String.valueOf(temp));
                break;
            }
            else
            {
                divideBy10Required++;
                //number=number/10;
                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;
                
               System.out.println("WHAT IS HAS ADJUST: " + hasAdjust);
                if (numberPalindromeChecks>=(divideBy10Required/2) &&numberPalindromeChecks!=0 && !hasAdjust)
                {
                    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))
                    
                    System.out.println("Single digit remaining: " + (temp.toString().substring(temp.toString().length()-1)));
                    isPalindrome=true;
                    break;
                }
                
            }
            //lastDigit=temp%10;
            lastDigit=new BigInteger(temp.toString().substring(temp.toString().length()-1));
            
            /*  Unsure if required
            if ((lastDigit%10)==0)
            {
                lastDigit=number;
            }
            */
            System.out.println("Current number: " + temp);
            //backupTemp=temp;
            backupTemp = new BigInteger(String.valueOf(temp));
            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;
            }
            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= (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);
            }
            firstDigit=temp;
            
            if (i>0)
            {
                if(!temp.toString().substring(0,temp.toString().length()-1).isEmpty())
                //if (temp%10!=0)
                {
                    //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);
            
            /*
            //if (firstDigit%10==0)
            if(firstDigit.toString().substring(0,firstDigit.toString().length()-1).isEmpty())
            {
                System.out.println("Should not reach here");
                
                firstDigit=temp;
                //break;
            }
            */
            
            //if (firstDigit==lastDigit)
            if(firstDigit.equals(lastDigit))
            {
                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);
            
            temp=backupTemp;
            //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("k: " + k);
            System.out.println("moveposition:" + k);
            System.out.println("i:" + i);
            System.out.println("divideBy10Required:" + divideBy10Required);
            
            
            //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 (movePosition>1)
            {
            if (k==movePosition && k==divideBy10Required && ((i+1==k)||((i+2)==k)||(i-1==k)) && evenFlag)
            {
                if (i-1==k)
                {
                    i=i-4;
                }
                else
                {
                System.out.println("00000000000000000000000000000000");
                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==k) && oddFlag)
            {
                System.out.println("0000000000000000000000000000000011111");
                i--;
                i--;
                adjustedI=true;
            }
            
            
            if (k==movePosition && k==divideBy10Required && (((i+1)==k)||((i-1)==k)) && oddFlag)
            {
                if((i+1)==k)
                {
                System.out.println("99999999999999999999999");
                i--;
                i--;
                
                }
                //a larger number of subtractions if i is larger than k
                else
                {
                    i=i-4;
                }
                adjustedI=true;
            }
            }
            
            
            /*
            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
    }
}