/*
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 :)");
        
	  //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)
    {
        //int lastDigit;
        BigInteger lastDigit;
        //int temp;
        BigInteger temp;
        //int backupTemp;
        BigInteger backupTemp = new BigInteger((pal.toString()));
        temp=pal;
        //int firstDigit=0;
        BigInteger firstDigit;
        
        boolean hasAdjust=false;
        int k=0;
        
        int divideBy10Required=0;
        
        boolean evenFlag=false;
        boolean oddFlag=false;
        
        int movePosition=0;
        int numberPalindromeChecks=0;
        int m=0;
        
        //For loop has been changed to do while loop to satisfy these previous for loops. 
        //They are no longer applicable (DO NOT ENABLE=> //for (m=0; m<10; m++)  //for (m=0; m<backupTemp.toString().length(); m++)
        do
        {
            m++;
        
            //if ((int)(number/10)==0)
            //if(pal.toString().substring(0,pal.toString().length()-1).isEmpty())
            if(pal.divide(new BigInteger("10")).compareTo(new BigInteger("0"))==0)
            {
                //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));
                pal=pal.divide(new BigInteger("10"));
            }
        }while (pal.compareTo(new BigInteger("0"))!=0);
       //while(m<10);
       //while(m<backupTemp.toString().length());
        
        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++)
        {
            if (evenFlag)
            {
                System.out.println("-----number checks complete: " + numberPalindromeChecks);
                
                if (numberPalindromeChecks>=(divideBy10Required/2) &&numberPalindromeChecks!=0 && !hasAdjust)
                {
                    System.out.println(temp);
                    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));
            lastDigit=temp.mod(new BigInteger("10"));
            
            System.out.println("Current number: " + temp);
            
            //backupTemp=temp;
            backupTemp = new BigInteger(String.valueOf(temp));
            
            hasAdjust = false;
            
            if (evenFlag)
            {
                System.out.println("MOVE POS: " + 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);
            
            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));
                temp = temp.divide(new BigInteger("10"));
                
                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)
                if(temp.mod(new BigInteger("10")).compareTo(new BigInteger("0"))!=0);
                
                {
                    //firstDigit= temp%10
                    //firstDigit=new BigInteger(temp.toString().substring(temp.toString().length()-1));
                    firstDigit=(temp.mod(new BigInteger("10")));
                }
            }
            System.out.println("This is first digit: " + firstDigit);
            System.out.println("This is last digit: " + lastDigit);
            
            //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("movePosition:"  + movePosition);
            
            temp=backupTemp;
            
            //temp = (int)(temp/10);
            temp = temp.divide(new BigInteger("10"));
            
            System.out.println("----------------------");
            System.out.println("New number going forward: " + temp);
           
            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
                    {
                        i--;
                        i--;
                    }
                }
            
                if (k==movePosition && k==divideBy10Required && (i==k))
                {
                    i--;
                    i--;
                }
            
                if (k==movePosition && k==divideBy10Required && (((i+1)==k)||((i-1)==k)) && oddFlag)
                {
                    if((i+1)==k)
                    {
                        i--;
                        i--;
                    }
                    else
                    {
                        i=i-4;
                    }
                }
            }
            numberPalindromeChecks++;
        }
    }
}