/*
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 :)");

	   //NO PALINDROME
	    //number=142734241;   //(EVEN FLAG)    - 10 digits  WRONG
	    //number=542734245;   //(EVEN FLAG)    - 9 digits   WRONG
	    //number=54243245;   //(ODD FLAG)      - 8 digits   CORRECT
        //number=54243245;   //(ODD FLAG)      - 7 digits   CORRECT
        //number=124321;      //(ODD FLAG)     - 6 digits   CORRECT
        //number=67896;       //(EVEN FLAG)    - 5 digits   CORRECT
        //number=1231;       //(EVEN FLAG)     - 4 digits   CORRECT
        //number=124;       //(EVEN FLAG)      - 3 digits   CORRECT
        //number=63;        //ODD FLAG           - 2 digits   CORRECT
        
        //PALINDROME
        
        //PALINDROME - EXPLORING BEYOND 5 DIGITS WIDE
         number=134625431;     //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
          //number=2349432;      //EVEN FLAG    7 digits  FAIL  FAIL
          //number=2341432;  //EVEN FLAG             -6 digits FAIL
         //number=56165;   // EVEN FLAG            - 5 digits  (not shown single digit but correct)
         //number=1331;     //ODD FLAG             - 4 digits  (WRONG)
        //number=888;      //EVEN FLAG            - 3 digits
         //number=121;      // EVEN FLAG           - 3 digits
         //number=44;       //ODD FLAG             - 2 digits
        //number=9;        //EVEN FLAG            - 1 digit
        
        
        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 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;
        }
        
        if (divideBy10Required%2!=0)
        {
            oddFlag=true;
        }
        
        for (int i=0; i<divideBy10Required; i++)
        {
            if (evenFlag)
            {
                if (numberPalindromeChecks>=(int)(divideBy10Required/2) && numberPalindromeChecks!=0)
                {
                    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)
            {
                if (movePosition>=8)
                {
                    movePosition = divideBy10Required-2;
                    hasAdjust=true;
                }
                
                if (movePosition>=6 &&!hasAdjust)
                {
                    movePosition = (divideBy10Required/2)+1;
                    hasAdjust=true;
                }
                
                if(movePosition<6 && !hasAdjust)
                {
                    movePosition = (divideBy10Required/2);
                }
            }
            else
            {
                movePosition = movePosition - 2;
            }
            
            if (i==0)
            {
                movePosition = divideBy10Required;
            }
            divideBy10Required=movePosition;
            System.out.println("Number moves required:" + movePosition);
            
            for (int 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;
            }
            
            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++;
        }
    }
}