Program to find the largest palindrome in an array | faceprep

Program to find the largest palindrome in an array is discussed below. Before we get into the problem, you need to know what a palindrome is. A palindrome is basically any text, numbers or a combination of both, which when read from first or last produces the same output. An example would be 

"10101"     "984565489"     "Radar"    "Madam"

Problem:

Given an array of non-negative integers arr[]. We need to find the largest palindrome in the array which is. If no such number exits then print -1.

Test cases:

Input:
4
5 131 54545  1111118

Output:
54545

Input:
6
2 4 5 8 25 61

Output:
8

Algorithm for finding the highest value palindrome is given below:

 

Largest Palindrome in an Array

// c program to find Largest palindrome in an array

#include

int check_palindrome(int n)
{
int div = 1;
while (n / div >= 10)
div *= 10;

while (n != 0)
{
int first = n / div;
int last = n % 10;

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

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

// Reducing divisor by a factor of 2 as 2 digits are dropped
div = div / 100;
}
return 1;
}
int large_palindrome(int A[], int n)
{

// Sort the array
for(int i=0; i<=n; i++)
{
for(int j=i; j<= n; j++)
{
if(A[i] >A [j])
{
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
}

for(int i=0; i<n; i++)
{
printf(“%d “, A[i]);
}

for (int i = n – 1; i >= 0; –i)
{
if (check_palindrome(A[i]) == 1)
return A[i];
}
return -1;
}
int main()
{
int a[15], n, i;
printf(“Enter the number of entries: \n”);
scanf(“%d”, &n);
printf(“Enter the elements: \n”);
for(i=0; i<n; i++)
scanf(“%d”, &a[i]);
printf(“\n Largest Palindrome: %d”, large_palindrome(a, n));
return 0;
}

// c++  program to find Largest palindrome in an array

#include <bits/stdc++.h>
using namespace std;
bool check_palindrome(int n)
{
int div = 1;
while (n / div >= 10)
div *= 10;

while (n != 0)
{
int first = n / div;
int last = n % 10;

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

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

// Reducing divisor by a factor of 2 as 2 digits are dropped
div = div / 100;
}
return true;
}
int large_palindrome(int A[], int n)
{

// Sort the array
sort(A, A + n);

for (int i = n – 1; i >= 0; –i)
{
if (check_palindrome(A[i]))
return A[i];
}
return -1;
}
int main()
{
int a[15], n, i;
cout<<“Enter the number of entries:”;
cin>>n;
cout<<“Enter the elements:”<<endl;
for(i=0; i<n; i++)
cin>>a[i];
cout <<“Largest Palindrome: “<< large_palindrome(a, n);
return 0;
}

// Java program to find Largest palindrome in an array

import java.util.*;
public class Main
{
public static boolean check_palindrome(int n)
{
int div = 1;
while (n / div >= 10)
div *= 10;

while (n != 0)
{
int first = n / div;
int last = n % 10;

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

// Removing the leading and trailing digits from the number
n = (n % div) / 10;
// Reducing divisor by a factor of 2 as 2 digits are dropped
div = div / 100; }return true; }
public static int large_palindrome(int A[], int n)
{ Arrays.sort(A);
for (int i = n – 1; i >= 0; –i) { if (check_palindrome(A[i]))
return A[i];
}
return -1;
}

public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int num,i;
System.out.println(“Enter the number of entries: “);
num = sc.nextInt();
int a[] = new int[num];
System.out.println(“Enter the elements: “);
for(i=0; i<num; i++)
{
a[i] = sc.nextInt();
}
System.out.println(“Largest Palindrome: “);
System.out.print(large_palindrome(a,num));
}
}

3 Python program to find Largest palindrome in an array

def reverse(string_num):
return string_num[::-1]

def isPalindrome(string_num):
rev = reverse(string_num)
if (string_num == rev):
return True
return False

def large_palindrome(A, n):
A.sort()
for i in range(n-1, 0, -1):
string_num = str(A[i])
if isPalindrome(string_num) == 1:
return A[i]
return -1

num = int(input(“Enter the number of entries: “))
a = []
print(“Enter the elements: “)
for i in range (num):
a.append(int(input()))
print(“Largest Palindrome: %d” %(large_palindrome(a,num)))

Output:

largest palindrome in an array

largest palindrome in an array