Finding the longest palindrome in an array

C++ Program to find the longest palindrome in the given array.

In the given array we need to find the longest palindrome number, A palindrome number is the number whose reverse number is same as the given number.


Lets we have an array A[] = {23, 675, 456654, 121, 290}, So in the given array 456654 and 121 are the two palindrome numbers and the 456654 is the longest one so , the output will be 456654.

Sorting element in array by frequency in C++
Longest Palindrome in an array in cpp


  • First take the value N , size of the array and elements of the array from the user. And create a variable let say max_palindrome, that keeps the track of the maximum palindrome number in the given array.
  • Sort the array using inbuilt sort() function.
  • Traverse the sorted array and check for every ith value, that it is palindrome or not. If the ith value of an array is  palindrome than we will update the max_palindrome variable.
  • After travensing the entire array we will get our maximum palindrone number.

C++ program based on above approach :

using namespace std;

bool isPalindrome (int N)
{ // function to check whether a number is Palindrome or not

int rev = 0, rem;

int temp = N;

while (temp)
rem = temp % 10;
rev = rev * 10 + rem;
temp /= 10;

if (N == rev)
return 1;

return 0;

int main ()

int N; // variable for size of the array

cout << "Enter the size of the array: ";
cin >> N;

int A[N]; // Declaration of an array of size N

cout << "\nInput the elements of the array: ";

for (int i = 0; i < N; i++)
cin >> A[i];

sort (A, A + N); // Sort the array A in ascending order

int result; // variable that store the final output

for (int i = 0; i < N; i++)

// check every ith value is Palindrome or not

if (isPalindrome (A[i]))
result = A[i];


cout << "\nThe longest palindrome number is : " << result;

return 0;
Enter the size of array : 5
Enter the elements of the array:

12 121 3 673 12121


The longest palindrome number is : 12121