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

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 :)");
        
        //if ((numberPalindromeChecks+1)>=(int)(divideBy10Required/2) && numberPalindromeChecks!=0)
                
        
	    //NO PALINDROME
	     //number=142734241;   //(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=134626431;     //EVEN FLAG   //10 digits  (number less than Java limit 2,147,483,647) - FAIL
           //number=542343245;    //EVEN FLAG   //9 digits  // NEED MORE CODE - FAIL
           //number=54244245;     //ODD FLAG   //8 digits  (it reaches single digit remaining early)
           //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
        
        
        System.out.println("\nThis is initial number: " + number);
        palindrome(number);
        
        System.out.println("------------------------------------------");
        System.out.println(number +  " is a palindrome:   " + isPalindrome);
        System.out.println("------------------------------------------");
    }
    
    public static void palindrome(int number)
    {
        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=number;
        
        for (m=0; m<10; m++)
        {
            if ((int)(number/10)==0)
            {
                number=temp;
                break;
            }
            else
            {
                divideBy10Required++;
                number=number/10;
            }
        }
        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 (originalNumberMovesRequired>=4)
                {
                    if (numberPalindromeChecks>(divideBy10Required/2) &&numberPalindromeChecks!=0 && !adjustedI)
                    {
                    System.out.println(temp);
                    
                    System.out.println("Single digit remaining: " + (temp%10));
                    isPalindrome=true;
                    break;
                    }
                }
                
                
                else
                {
                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))
                    
                    System.out.println("Single digit remaining: " + (temp%10));
                    isPalindrome=true;
                    break;
                }
                }
            }
            lastDigit=temp%10;
            
            if ((lastDigit%10)==0)
            {
                lastDigit=number;
            }
            System.out.println("Current number: " + temp);
            backupTemp=temp;
            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;
            }
            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);
                System.out.println("NEW NUMBER (TO EXPOSE FIRST DIGIT ON RIGHT HANDSIDE): " + temp);
            }
            firstDigit=temp;
            
            if (i>0)
            {
                if (temp%10!=0)
                {
                    firstDigit= temp%10;
                }
            }
            System.out.println("This is first digit: " + firstDigit);
            System.out.println("This is last digit: " + lastDigit);
            
            if (firstDigit%10==0)
            {
                System.out.println("Should not reach here");
                
                firstDigit=temp;
                //break;
            }
            
            if (firstDigit==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);
            
            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 (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) && 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;
            }
            
            
            /*
            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
    }
}