Finding the Longest Palindrome in an Array
Detailed guide and Python implementation for the 'Finding the Longest Palindrome in an Array' problem.
Problem Statement
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.
- •1 <= len(arr) <= 10^5
- •1 <= arr[i] <= 10^9
Examples
arr = [12, 121, 33, 456, 1331]
1331
Palindromic numbers: 121, 33, 1331. The largest is 1331.
arr = [10, 20, 30]
-1
None of 10, 20, 30 are palindromes (10 reversed is 01 which is 1, not 10).
arr = [7, 11, 22, 123]
22
Palindromic numbers: 7, 11, 22. The largest is 22.
Need a Hint?
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.
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Try/Except & Error Handling
Prevent your Python scripts from crashing. Learn try, except, finally blocks and how to raise custom exceptions properly.
How to Reverse a String in Python
Learn how to reverse a string in Python using slicing, the reversed() function, and loop concatenation with visual code examples.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.