Back to Practice Dashboard
Python BasicsEasy
Finding the Longest Palindrome in an Array
Learn how to solve the 'Finding the Longest Palindrome in an Array' problem. This detailed resource details brute force and optimized approaches.
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?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.