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

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

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


  • sagarika

    import java.util.*;
    public class LargestPalindrome {
    public static void main(String args[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.println(“enter size of array”);
    int size=sc.nextInt();
    int arr[]=new int[size];
    System.out.println(“enter array elements”);
    for(int i=0;i<size;i++)
    {
    arr[i]=sc.nextInt();
    }
    int larger=SortArray(arr,size);
    if(larger==-1)
    {
    System.out.println("no palindrome in array");
    }
    else
    {
    System.out.println("longest palindrome in array is: "+larger);
    }

    }
    public static int SortArray(int arr[],int n)
    {
    int temp;
    for(int i=0;i<n;i++)
    {
    for(int j=i+1;jarr[j])
    {
    temp=arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
    }
    }
    }
    for(int i=n-1;i>=0;i–)
    {

    if(isPalindrome(arr[i]))
    {
    return arr[i];
    }

    }
    return -1;

    }
    public static boolean isPalindrome(int n)
    {
    int temp,rev=0,rem;
    temp=n;
    while(n!=0)
    {
    rem=n%10;
    rev=rev*10+rem;
    n=n/10;
    }
    if(rev==temp)
    {
    return true;
    }
    else
    return false;

    }
    }


  • Khushi Saxena

    class HelloWorld {
    public static void main(String[] args) {
    int a[]={545,5445,54545,111,222};
    int n=a.length;
    System.out.println(largepalid(a,n));
    }
    static int largepalid(int a[],int n){
    int res=-1;
    for(int i=0;ires && palind(a[i]))
    res=a[i];

    }
    return res;
    }
    static boolean palind(int n){
    int divisor=1;
    while(n/divisor>=10){
    divisor*=10;
    }
    while(n!=0){
    int x=n/divisor;
    int y=n%10;

    if(x!=y){
    return false;
    }

    n=(n/divisor)/10;

    divisor=divisor/100;

    }
    return true;
    }
    }
    The time complexity of the given code is O(d * n), where ‘d’ represents the number of digits in the largest number in the array, and ‘n’ represents the number of elements in the array.


  • prakhar

    import java.util.*;
    public class Main
    {
    public static boolean CheckPalindrome(int n){
    int temp=n;
    int rev=0;
    while(n>0){
    int rem=n%10;
    rev=rev*10+rem;
    n=n/10;
    }
    if(rev==temp){
    return true;
    }
    else{
    return false;
    }
    }
    public static void main(String[] args) {
    int arr[]={ 121,54545,2322, 999990 };
    Arrays.sort(arr);
    for(int i=arr.length-1;i>=0;i–){
    if(CheckPalindrome){
    System.out.println(arr[i]);
    break;
    }
    }

    }
    }


  • Gokul

    // Online Java Compiler
    // Use this editor to write, compile and run your Java code online

    class HelloWorld {

    public static int[] SortArray()
    {
    int arr[]={2,1,3,4,5,50};
    int len=arr.length;
    int temp;
    for(int i=0;i<len;i++)
    {
    for(int j=i+1;jarr[j])
    {
    temp=arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
    }
    }
    }

    return arr;
    }

    static boolean checkPalin(int num)
    {
    int rev=0,temp=num;

    if(num<=9)
    return true;
    while(num!=0)
    {
    int rem=num%10;
    rev=rev*10+rem;
    num/=10;
    }

    return temp==rev;
    }

    public static void main(String[] args) {

    int a[]=SortArray();
    int log_palin=-1;

    for(int i=0;i<a.length;i++)
    {
    if(checkPalin(a[i]))
    {
    log_palin=a[i];
    }
    }
    System.out.print(log_palin);
    }

    }


  • Gyanendra

    import java.util.*;
    public class Main{
    public static void main(String[] args){
    Scanner scn=new Scanner(System.in);
    int n=scn.nextInt();
    int[] arr=new int[n];

    for(int i=0;i<n;i++){
    arr[i]=scn.nextInt();
    }

    int ans=0;

    for(int i=0;i0){
    int div=temp%10;
    nval=nval*10+div;
    temp/=10;
    }

    if(nval==o_val){
    if(ans<o_val)
    ans=o_val;
    }

    }

    System.out.println(ans);
    }
    }


  • Ganpat

    // Find longest palindrone
    // Easy and short method
    import java.util.*;
    class Main {

    static int isPalindrome(int n, int rev){

    if(n == 0)
    return rev;

    int rem = n % 10;
    rev = rev * 10 + rem;
    return isPalindrome(n/10, rev);
    }

    public static void main(String [] args){

    int arr[] = {121,7557,55255,1221,454,525};
    int len = arr.length;
    int rev= 0, rem;

    Arrays.sort(arr);
    for(int i= len-1; i>=0; i++){
    if(isPalindrome(arr[i], rev)==arr[i]){
    System.out.println(arr[i]);
    break;
    }
    }
    }
    }


  • Gyanendra

    import java.util.*;
    public class Find_longest_paillindrom_number_in_an_array {

    public static void main(String[] args) {
    Scanner scn =new Scanner(System.in);
    int n=scn.nextInt();
    int[] arr=new int[n];
    for(int i=0;i=0;i–) {

    if(pailindrom(arr[i])==true) {
    System.out.println(arr[i]);
    return;
    }
    }
    System.out.println(-1);
    }

    public static boolean pailindrom(int n) {
    int temp=n,val=0;

    while(temp>0) {
    val=(val*10)+temp%10;
    temp/=10;
    }
    if(val==n) {
    return true;
    }
    else {
    return false;
    }

    }

    }


  • 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");
    }

    }}