Largest palindromic number in an array | Function to check if n is palindrome ; Find the appropriate divisor to extract the leading digit ; If first and last digits are not same then return false ; Removing the leading and trailing digits from the number ; Reducing divisor by a factor of 2 as 2 digits are dropped ; Function to find the largest palindromic number ; Sort the array ; If number is palindrome ; If no palindromic number found ; Driver Code ; print required answer
< ? php function isPalindrome ( $ n ) { $ divisor = 1 ; while ( ( int ) ( $ n / $ divisor ) >= 10 ) $ divisor *= 10 ; while ( $ n != 0 ) { $ leading = ( int ) ( $ n / $ divisor ) ; $ trailing = $ n % 10 ; if ( $ leading != $ trailing ) return false ; $ n = ( int ) ( ( $ n % $ divisor ) / 10 ) ; $ divisor = ( int ) ( $ divisor / 100 ) ; } return true ; } function largestPalindrome ( $ A , $ n ) { sort ( $ A ) ; for ( $ i = $ n - 1 ; $ i >= 0 ; -- $ i ) { if ( isPalindrome ( $ A [ $ i ] ) ) return $ A [ $ i ] ; } return -1 ; } $ A = array ( 1 , 232 , 54545 , 999991 ) ; $ n = sizeof ( $ A ) ; echo largestPalindrome ( $ A , $ n ) ; ? >