Python BasicsEasy

Finding the Longest Palindrome in an Array

Detailed guide and Python implementation for the 'Finding the Longest Palindrome in an Array' problem.

Problem Statement

Easy

Write a function longest_palindrome(arr) that takes a list of positive integers and returns the largest number in the array whose digits form a palindrome. A palindromic number reads the same forwards and backwards (e.g., 121, 1331, 7). If no palindromic number exists, return -1.

Constraints
  • 1 <= len(arr) <= 10^5
  • 1 <= arr[i] <= 10^9

Examples

Example 1
Input
arr = [12, 121, 33, 456, 1331]
Output
1331
Explanation

Palindromic numbers: 121, 33, 1331. The largest is 1331.

Example 2
Input
arr = [10, 20, 30]
Output
-1
Explanation

None of 10, 20, 30 are palindromes (10 reversed is 01 which is 1, not 10).

Example 3
Input
arr = [7, 11, 22, 123]
Output
22
Explanation

Palindromic numbers: 7, 11, 22. The largest is 22.

Need a Hint?
Consider using Arrays-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.