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

//for instance    initial number as : 56165     
//(Initial number of times divideBy10Required is 4 (even number)  in order to expose firstDigit and lastDigit)
//once the  lastDigit is truncated,  it would result in:    5616
//number of times divideBy10Required is require to expose firstDigit (6)  and lastDigit (6) is now halved..
//it would then calculate numberExecutions is more than or equal to half of divideBy10Required.
//it would ascertain that there is a single digit.
//assuming currently isPalindrome=true,  it can then ascertain number to be a palindrome.
// IT CAN BE SEEN THERE IS A HALVING PRINCIPLE TO EXECUTIONS OF divideBy10Required

//for instance    initial number as : 561165
//(Initial number of times divideBy10Required is 5 (odd number)  in order to expose firstDigit and lastDigit)
//once the  lastDigit is truncated,  it would result in:    56116
//number of times divideBy10Required is require to expose firstDigit (6)  and lastDigit (6) is now 3.
//IT IS NOT HALVING EFFECT, THERE IS SUBTRACTION BY 2.
//IT WOULD THEN TRUNCATE AS FOLLOWS   5611    (lastDigit=1 and firstDigit=1)
//It has performed 2 x numberExecutions and it will be left with two digits remaining..
//In ALL circumstances, there would be two numbers remaining.
//Other examples include     54211245

//I attempted this challenge using recursion, but there was so much logic appearing
//it became progressively difficult.
//So this is completed without recursion.

public class Main
{
    static int lastDigit;  //last digit once truncation has occurred
    static int divideBy10Required;   //critical variable frequency of /10 to expose firstDigit with lastDigit
    static int temp;  //it will keep truncated version of the number
    static int number;  //input provided by end user
    static boolean processCheckPalindrome;  //sets flag once processing starts, intended purpose was prevent repeating code with recursion.
    static boolean palindromeCheck; //once again, purpose to prevent repeat code execution with recursion.
    static int startPosI;  //again, it was used for purpose of recursion. Never proceeded with recursion.
    static int startPosj;  //again, it was used for purpose of recursion. Never proceeded with recursion.
    static int backupTemp; //keeps a copy, so that on next /10, it can perform on the previous version.
    static boolean evenFlag; //acknowledge if divideBy10Required is even
    static boolean oddFlag;  //acknowledge if divideBy10Required is odd
    
    //static int lastDigit;
    static int firstDigit=0;
    static boolean isPalindrome=false;
    static int movePosition=0;
    static int numberPalindromeChecks=0;
    
    
    
    public static void main(String[] args) {
    System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
        //number=542645;   // odd number of divideBy10Required divideBy10Required
        //number=56165;   // even number of divideBy10Required divideBy10Required
        
        //number=121;
        number=44;
        
        //method call
        palindrome(number);
    }
    
    public static int palindrome(int number)
    {
        //it would process this once only per recursion call....
        //I had these flags set initially.
        //recursion was not used, so in practice whole loop can be removed
        //I left it on to segregate logic.
        if (!palindromeCheck  && !processCheckPalindrome)
        {
            //this assigns the number into temp variable
            temp=number;
        
        // m can be set to limit of digits in int
        //alternatively if long is used, value of m can be up to  
        //9,223,372,036,854,775,807
        //this is maximum of 20
        
        for (int m=0; m<=20; m++)
        {
            System.out.println("This is number: " + number);
        
        //at this point is has reached the firstDigit
        if ((int)(number/10)==0)
        {
            //it has to restore the number back to original state
            number=temp;
            
            break;
        }
        
        // this is almost unorthodox technique to perform recursion for truncating the end digit
        //chosen not to perform another recursive method notably given that
        //!PalindromeCheck will only execute once.
        //it is restricted to a number 64 digits wide
        
        else
        {
            divideBy10Required++;
            number=number/10;
        }
        
        System.out.println("It will take this many divisions to reach start to end: " + divideBy10Required);
        }
        }
        
        if (divideBy10Required%2==0)
        {
            evenFlag=true;
        }
        
        if (divideBy10Required%2!=0)
        {
            oddFlag=true;
        }
        
        palindromeCheck=true;
        
        //it has to perform the recursion call here..
        //We know on first instance, it will need to perform
        //   /10     divideBy10Required times
        // if it satisfies palindrome check, then 
        //divideBy10Required -1 
        
      temp=number;
        
        System.out.println("***********");
        
        for (int i=0; i<divideBy10Required; i++)
        {
            //This is definitely applicable when evenFlag is set to True
            if (evenFlag)
            {
               if (numberPalindromeChecks>=(int)(divideBy10Required/2) && numberPalindromeChecks!=0)
            {
                System.out.println("Single digit remaining");
                System.exit(0);
            }
            }
            
            lastDigit=temp%10;
            
            if ((lastDigit%10)==0)
            {
                System.out.println("should not");
                lastDigit=number;
            }
            
            System.out.println("value of i: " + i);
            System.out.println("This is last digit: " + lastDigit);
            
            processCheckPalindrome=true;
            //number=temp;
            
            for (int j=i; j<divideBy10Required; j++)
            {
            //at this point there will be a single number left
            //which has not been processed.
            //all numbers around it have validated for being a palindrome....
                
                if (j==(divideBy10Required-1))
                {
                    
                System.out.println("curret number: " + temp);
                
                //it should reduce it now for comparison.....
                
                backupTemp=temp;
                
                if (evenFlag)
                {
                movePosition = divideBy10Required/2;
                }
                
                else
                {
                    movePosition = movePosition - 2;
                }
                
                //this will override above, if required...
                if (i==0)
                {
                    movePosition = divideBy10Required;
                }
                
                for (int k=0; k<movePosition; k++)
                {
                    System.out.println("HOW MANY!!!: " +  (movePosition));
                    
                    temp= (int) (temp/10);
                    System.out.println("NEW NUMBER: " + temp);
                }
                
                System.out.println("value of j: " + j);
                
                firstDigit=temp;
                
                //slightly different approach in getting the number to compare against
                //end element.
                //this was also documented since there is no truncation at front...
                
                if (i>0)
                {
                    //will ensure it gets last digit only of the front section
                    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)
                {
                    firstDigit=number;
                }
            
            if (firstDigit==lastDigit)
            {
                System.out.println("PALINDROME: " + firstDigit + "  " + lastDigit);
                isPalindrome=true;
                
            }
            else
            {
                isPalindrome=false;
                System.out.println("NOT PALINDROME");
                System.exit(0);
                
            }  //end else 
                }
            }//end for loop
            
            //it can perform palindrome check here..
            
            temp=backupTemp;
            temp = (int)(temp/10);
            
            System.out.println("NEW NUMBER GOING FORWARD: " + temp);
            
            numberPalindromeChecks++;
            
        }  //end outer for loop...
        
        return 1;
    }   //end method
}