Java program for longest palindrome in an array

Longest Palindrome in an Array in Java

Here, in this page we will discuss the program to find the longest palindrome in an array in java programming language. We are given with an array and need to print the longest palindromic element in the given input array.

java code Longest palindrome in an array

Method Discussed :

  • Method 1 : Using Naive Approach
  • Method 2 : Using Sorting

Method 1 :

  • Create a function ispalindrome(int n), it will return 1 if the passing number is palindrome otherwise return 0.
  • Inside the main function, create a variable say res = -1, that hold the maximum palindrome number.
  • Run a loop from [0, n),  and check if(ispalindrome(arr[i]) && res<arr[i]), then set res = arr[i].
  • Print the value of res.

Method 1 : Code in Java

Run
import java.util.*;

class Main
{
    // Function to check if n is palindrome
    static boolean isPalindrome(int n)
    {
          // Find the appropriate divisor
          // to extract the leading digit
          int divisor = 1;
          while (n / divisor >= 10)
             divisor *= 10;

          while (n != 0) {
             int x = n / divisor;
             int y = n % 10;

             // If first and last digits are
             // not same then return false
             if (x != y)
               return false;

             // Removing the leading and trailing
             // digits from the number
             n = (n % divisor) / 10;

             // Reducing divisor by a factor
             // of 2 as 2 digits are dropped
            divisor = divisor / 100;
         }
         return true;
    }

    // Function to find the largest palindromic number
    static int largestPalindrome(int []A, int n)
    {
         int res = -1;

         for (int i = 0; i < n; i++) { // If a palindrome larger than the currentMax is found 
                 if (A[i] > res && isPalindrome(A[i]))
                     res = A[i];
         }

        // Return the largest palindromic number from the array
        return res;
     }

    // Driver program
    public static void main(String []args)
    {
       int []A = { 121, 2322, 54545, 999990 };
       int n = A.length;

      // print required answer
      System.out.println(largestPalindrome(A, n));
    }

}

Output

54545

Method 2 :

In this method we first sort the array and then start traversing the array from end, and return the element that satisfy the condition, that it is palindrome.

  • Sort the array using inbuilt sort() function.
  • Start traversing from end of the array,
  • Print the element if it is palindrome.
  • If no element found then print -1.
Longest Palindrome in Java

Method 2 : Code in Java

Run
import java.util.*;
class Main
{
// Function to check if n is palindrome
static boolean isPalindrome(int n)
{
// Find the appropriate divisor
// to extract the leading digit
int divisor = 1;
while (n / divisor >= 10)
divisor *= 10;

while (n != 0) {
int x = n / divisor;
int y = n % 10;

// If first and last digits are
// not same then return false
if (x != y)
return false;

// Removing the leading and trailing
// digits from the number
n = (n % divisor) / 10;

divisor = divisor / 100;
}
return true;
}

// Function to find the largest palindromic number
static int largestPalindrome(int []A, int n)
{
Arrays.sort(A);
for (int i = n-1; i >= 0; i--) {
// If a palindrome larger than the currentMax is found
if (isPalindrome(A[i]))
return A[i];
}

// Return the largest palindromic number from the array
return -1;
}

// Driver program
public static void main(String []args)
{
int []A = { 121, 2322, 54545, 999990 };
int n = A.length;

// print required answer
System.out.println(largestPalindrome(A, n));
}

}

Output

545545

3 comments on “Java program for longest palindrome in an array”


  • Dhivya

    import java.util.Scanner;

    public class palindrome_number {
    public static boolean isplaindrome(int a){
    int num=a;
    int res=0;
    int rem=0;
    while(num !=0){
    rem= num%10;
    res= (res*10)+rem;
    num=num/10;
    }
    if( res == a){
    return true;
    }
    else
    return false;
    }
    public static int largestPalindrome(int arr[], int n){
    int c =-1;

    for(int i=0;ic && isplaindrome(arr[i])){
    c = arr[i];
    }

    }
    return c;
    }
    public static void main(String[] args) {
    Scanner s= new Scanner(System.in);
    int n= s.nextInt();
    int arr[]= new int[100];
    for( int i=0;i<n;i++)
    arr[i] = s.nextInt();
    System.out.println(largestPalindrome(arr,n));

    }
    }


  • Mahesh

    int[]a={11,333,22,55,45,232,545,66,456,454,5655,12,12232,45454,6766,454,344,2333,11111,123,34233,455};
    Arrays.sort(a);
    for(int i=a.length-1;i>=0;i–){
    if(isPalindrome(a[i])){
    System.out.println(a[i]);
    break;
    }
    }
    }
    static boolean isPalindrome(int n){
    String s=Integer.toString(n);
    int i=0,j=s.length()-1;
    while(i<j){
    if(s.charAt(i)!=s.charAt(j)){
    return false;
    }
    i++;
    j–;
    }
    return true;
    }


  • lasic

    import java.util.Arrays;
    import java.util.Scanner;

    public class array_palindrome {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter the Range”);
    int n = sc.nextInt();
    int a[] = new int[n];
    int z[]=new int[n];
    int q=0;
    for (int i = 0; i < n; i++) {
    a[i] = sc.nextInt();
    }
    for(int i=0;i<n;i++)
    {
    int b=a[i];
    String s="";
    while(b!=0)
    {
    int remainder=b%10;
    s= s +remainder;
    b=b/10;
    }
    if(a[i] == Integer.parseInt(s))
    {
    z[q]=a[i];
    q++;
    }
    }
    Arrays.sort(z);
    try {
    System.out.println(z[q – 1]);
    } catch (ArrayIndexOutOfBoundsException e) {
    System.out.println("There is no palindrome");
    }

    }}