Java program for longest palindrome in an array

Longest palindrome in an array.

Here we find the longest palindrome number from the given array. So here we need to sort the array in ascending order and later check from the last that the given number is palindrome or not.

The reason behind to sort an array is, we want the longest palindrome number so, in the sorted array, we got the largest number from the last element of the array, and check this is a palindrome or not and check till starting element. If there is no palindrome number so return -1 and print that there is no palindrome number.

Example

Input a[] = {1, 232, 54545, 999991};

Output: 54545

 

Longest palindrome in an array

Algorithm

Step 1. Initialize arrays.

Step 2. Declare the scanner class for taking input.

Step 3. take array size from the user.

Step 4. Take an element of the array from the user.

Step 5.  store the return value of function longest in a longest variable.

longest(int a[], int size)

Step 1. Initialize temp variable.

Step 2.  Initialize the two-loop for sorting.

Step 3.  perform sorting.

Step 4. Initialize backward for loop for checking the large palindrome number.

Step 5. Check the current index value is a palindrome or not to call the is palindrome Function. and return the current element.

Step 6. if there is not any palindrome number so return -1.

boolean is Palindrome(int n)

Step1. Copy the original number which is passed from the main function.

Step 2. Initialize while loop to find the reverse of the number.

Step 3. If the reverse of the number is equal to the original number then return true otherwise, return false. 

Java Code

import java.util.*;
public class Main
{
      public static boolean isPalindrome(int n) 
      {
             int remainder,number,reverse=0;
             number=n;
              //while loop for find the reverse of the number
             while(n!=0)
             {
                  remainder=n%10;
                  reverse=reverse*10+remainder;
                  n=n/10;
             }
              //check whether number is palindrome or not
             if(number==reverse)
                    return true;
             else
                   return false;
       }

       public static int longest(int []arr, int n) 
       {
             int i,j,temp;
               //sorting of number in ascending order
             for(i=0;i<n;i++)
             {
                 for(j=0;j<n;j++)
                 {
                     if(arr[i]<arr[j])
                     {
                         temp=arr[i];
                         arr[i]=arr[j];
                         arr[j]=temp;
                      }
                   }
                }
             //check from the last index of array that number is palindrome or not
            //start from last because we want longest palindrome number that is the reason of the sorting
            for (i = n - 1; i >= 0; --i) 
            { 
                 if (isPalindrome(arr[i])) 
                   return  arr[i]; 
            }
            return -1; 
       }
       public static void main(String[] args) 
       {
            int[] arr= new int[50];
            Scanner sc=new Scanner(System.in);
            System.out.println(      "enter size");
            int size=sc.nextInt();
            System.out.println(      "enter element");
            for(int i=0;i<size;i++)
            {
                 arr[i]=sc.nextInt();
            }
            int larger=longest(arr, size);
            if(larger==-1)
                 System.out.println(  "There is no palindrome number in the array"); 
            else
                 System.out.println(  "longest palindrome number in the array"+longest);
        }
}

OUTPUT-
CASE-1
ENTER SIZE-5
ENTER ELEMENT-
1
33
121
1111
12121
LONGEST PALINDROME IS 12121

CASE-2
ENTER SIZE-5
ENTER ELEMENTS-
31
24
32
69
79
There is no palindrome number