Find the Longest Palindrome in an Array using Python

Finding the Longest Palindrome in an Array using Python

Longest Palindrome in an Array

Today here we will learn how to find the Longest Palindrome in an Array using Python concept.The Palindrome means any text, numbers or a combination of both, which when read from first to last and last to first generates the same result.Here we have find longest palindrome number.
For Example
Enter the size of array:6
Enter the elements: 343
555
1
4554
121
232
The longest palindrome number is 4554

Algorithm For Longest Palindrome Number

  • First declare and initialize an array
  • Sort the array in ascending order.
  • Start traversing the array from the end.
  • The first number which is a longest palindrome is the required answer.
  • If no palindromic number is found then print no palindrome exists

Python Program to find Longest Palindrome Number in a Array

import sys
n=int(input("ENTER ARRAY SIZE "))
arr=[ ]
for i in range(n):
    element=int(input())
    arr.append(element)
arr.sort(reverse=True)
for i in arr:
    if str(i)==str(i)[::-1]:
        print("longest palindrome ",i)
        sys.exit(0)
print("no palindrome exists")
Output
ENTER ARRAY SIZE 5
454
22
1
5665
3321
longest palindrome 5665