/*
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   -  NEED MORE CODE
	    //number=542734245;   //(EVEN FLAG)    - 9 digits   -  NEED MORE CODE
         //number=54243245;   //(ODD FLAG)    - 8 digits
        //number=678;         //(EVEN FLAG)   - 3 digits
         //number=63;          //ODD FLAG     - 2 digits
        
        //PALINDROME
          //number=56165;   // EVEN FLAG      - 5 digits 
         //number=121;    // EVEN FLAG        - 3 digits
         //number=888;    //EVEN FLAG         - 3 digits
        //number=44;          //ODD FLAG        - 2 digits
        //number=9;        //ODD FLAG         - 1 digit
        
        //PALINDROME - EXPLORING BEYOND 5 DIGITS WIDE
         //number=134625431;     //EVEN FLAG   //10 digits  (number less than Java limit 2,147,483,647)
        number =  542343245;    //EVEN FLAG   //9 digits  // NEED MORE CODE
         //number =   54244245;       //ODD FLAG   //8 digits
          //number =    2349432;       //EVEN FLAG    7 digits
          //number =     2341432;       //EVEN FLAG    //6 digits
         //number =      56165;           //EVEN FLAG    //5 digits
        
        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 k=0;
        int lastDigit;
        int divideBy10Required=0;
        int temp;
        int backupTemp; 
        boolean evenFlag=false;
        boolean oddFlag=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: " + numberPalindromeChecks);
                System.out.println("-----divide by 10: " + divideBy10Required/2);
                if (((numberPalindromeChecks-1)==(int)(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)%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("**************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)
                {
                     System.out.println("3Move position adjusted");
                    movePosition = (divideBy10Required/2);
                    
                }
            }
            else
            {
                movePosition = movePosition - 2;
            }
            
            if (i==0)
            {
                movePosition = divideBy10Required;
            }
            divideBy10Required=movePosition;
            System.out.println("Number moves required:" + 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;
            }
            
            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);
            
            if (movePosition==3)
            {
                if (movePosition==i)
                {
                    System.out.println("Reduced i counter by 2");
                    i--;
                    i--;
                }
                else
                {
                    System.out.println("Reduced i counter by 1");
                    i--;
                }
            }
            numberPalindromeChecks++;
        }  //end for
    }
}