Largest palindromic number in an array

11 Mar, 2022
Given an array of non-negative integers arr[]. The task is to find the largest number in the array which is palindrome. If no such number exits then print -1.
Examples: 
Input: arr[] = {1, 232, 54545, 999991}; Output: 54545
Input: arr[] = {1, 2, 3, 4, 5, 50}; Output: 5  
Method 1:  
Sort the array in ascending order.
Start traversing the array from the end.
The first number which is a palindrome is the required answer.
If no palindromic number is found then print -1
Below is the implementation of the above approach: 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std; // Function to check if n is palindromebool isPalindrome(int n){    // Find the appropriate divisor    // to extract the leading digit    int divisor = 1;    while (n / divisor >= 10)        divisor *= 10;     while (n != 0) {        int leading = n / divisor;        int trailing = n % 10;         // If first and last digits are        // not same then return false        if (leading != trailing)            return false;         // Removing the leading and trailing        // digits from the number        n = (n % divisor) / 10;         // Reducing divisor by a factor        // of 2 as 2 digits are dropped        divisor = divisor / 100;    }    return true;} // Function to find the largest palindromic numberint largestPalindrome(int A[], int n){     // Sort the array    sort(A, A + n);     for (int i = n - 1; i >= 0; --i) {         // If number is palindrome        if (isPalindrome(A[i]))            return A[i];    }     // If no palindromic number found    return -1;} // Driver programint main(){    int A[] = { 1, 232, 54545, 999991 };    int n = sizeof(A) / sizeof(A[0]);     // print required answer    cout << largestPalindrome(A, n);     return 0;}
// Java implementation of above approach import java.util.*; class GFG{    // Function to check if n is palindrome    static boolean isPalindrome(int n)    {        // Find the appropriate divisor        // to extract the leading digit        int divisor = 1;        while (n / divisor >= 10)            divisor *= 10;             while (n != 0) {            int leading = n / divisor;            int trailing = n % 10;                 // If first and last digits are            // not same then return false            if (leading != trailing)                return false;                 // Removing the leading and trailing            // digits from the number            n = (n % divisor) / 10;                 // Reducing divisor by a factor            // of 2 as 2 digits are dropped            divisor = divisor / 100;        }        return true;    }         // Function to find the largest palindromic number    static int largestPalindrome(int []A, int n)    {             // Sort the array        Arrays.sort(A);             for (int i = n - 1; i >= 0; --i) {                 // If number is palindrome            if (isPalindrome(A[i]))                return A[i];        }             // If no palindromic number found        return -1;    }         // Driver program    public static void main(String []args)    {        int []A = { 1, 232, 54545, 999991 };        int n = A.length;             // print required answer        System.out.println(largestPalindrome(A, n));                  } } // This code is contributed// by ihritik
# Python3 implementation of above approach # Function to check if n is palindromedef isPalindrome(n) :         # Find the appropriate divisor    # to extract the leading digit    divisor = 1         while (n / divisor >= 10) :        divisor *= 10     while (n != 0) :                 leading = n // divisor        trailing = n % 10         # If first and last digits are        # not same then return false        if (leading != trailing) :            return False         # Removing the leading and trailing        # digits from the number        n = (n % divisor) // 10         # Reducing divisor by a factor        # of 2 as 2 digits are dropped        divisor = divisor // 100         return True # Function to find the largest# palindromic numberdef largestPalindrome(A, n) :     # Sort the array    A.sort()     for i in range(n - 1, -1, -1) :         # If number is palindrome        if (isPalindrome(A[i])) :            return A[i]         # If no palindromic number found    return -1 # Driver Codeif __name__ == "__main__" :     A = [ 1, 232, 54545, 999991 ]    n = len(A)     # print required answer    print(largestPalindrome(A, n)) # This code is contributed by Ryuga
// C# implementation of above approach using System; class GFG{    // Function to check if n is palindrome    static bool isPalindrome(int n)    {        // Find the appropriate divisor        // to extract the leading digit        int divisor = 1;        while (n / divisor >= 10)            divisor *= 10;             while (n != 0) {            int leading = n / divisor;            int trailing = n % 10;                 // If first and last digits are            // not same then return false            if (leading != trailing)                return false;                 // Removing the leading and trailing            // digits from the number            n = (n % divisor) / 10;                 // Reducing divisor by a factor            // of 2 as 2 digits are dropped            divisor = divisor / 100;        }        return true;    }         // Function to find the largest palindromic number    static int largestPalindrome(int []A, int n)    {             // Sort the array        Array.Sort(A);             for (int i = n - 1; i >= 0; --i) {                 // If number is palindrome            if (isPalindrome(A[i]))                return A[i];        }             // If no palindromic number found        return -1;    }         // Driver program    public static void Main()    {        int []A = { 1, 232, 54545, 999991 };        int n = A.Length;             // print required answer        Console.WriteLine(largestPalindrome(A, n));                  } } // This code is contributed// by ihritik
<?php// PHP implementation of above approach // Function to check if n is palindromefunction isPalindrome($n){    // Find the appropriate divisor    // to extract the leading digit    $divisor = 1;    while ((int)($n / $divisor) >= 10)        $divisor *= 10;     while ($n != 0)    {        $leading = (int)($n / $divisor);        $trailing = $n % 10;         // If first and last digits are        // not same then return false        if ($leading != $trailing)            return false;         // Removing the leading and trailing        // digits from the number        $n = (int)(($n % $divisor) / 10);         // Reducing divisor by a factor        // of 2 as 2 digits are dropped        $divisor = (int)($divisor / 100);    }    return true;} // Function to find the largest// palindromic numberfunction largestPalindrome($A, $n){     // Sort the array    sort($A);     for ($i = $n - 1; $i >= 0; --$i)    {         // If number is palindrome        if (isPalindrome($A[$i]))            return $A[$i];    }     // If no palindromic number found    return -1;} // Driver Code$A = array(1, 232, 54545, 999991);$n = sizeof($A); // print required answerecho largestPalindrome($A, $n); // This code is contributed// by Akanksha Rai?>
<script>//Javascript implementation of above approach //function for sortingfunction sort(a, n) {   var i, j, min, temp;   for (i = 0; i < n - 1; i++) {      min = i;      for (j = i + 1; j < n; j++)      if (a[j] < a[min])      min = j;      temp = a[i];      a[i] = a[min];      a[min] = temp;   }} // Function to check if n is palindromefunction isPalindrome(n){    // Find the appropriate divisor    // to extract the leading digit    var divisor = 1;    while (parseInt(n / divisor) >= 10)        divisor *= 10;     while (n != 0) {        var leading = parseInt(n / divisor);        var trailing = n % 10;         // If first and last digits are        // not same then return false        if (leading != trailing)            return false;         // Removing the leading and trailing        // digits from the number        n = parseInt((n % divisor) / 10);         // Reducing divisor by a factor        // of 2 as 2 digits are dropped        divisor = parseInt(divisor / 100);    }    return true;} // Function to find the largest palindromic numberfunction largestPalindrome( A, n){     // Sort the array    sort(A,A.length);     for (var i = n - 1; i >= 0; --i) {         // If number is palindrome        if (isPalindrome(A[i]))            return A[i];    }     // If no palindromic number found    return -1;} var  A = [ 1, 232, 54545, 999991 ];var n = A.length; // print required answer document.write(largestPalindrome(A, n));  //This code is contributed by SoumikMondal</script>
54545
 
Method 2: 
Set a variable currentMax = -1 and start traversing the array.
If current element arr[i] > currentMax and arr[i] is a palindrome.
Then set currentMax = arr[i].
Print currentMax in the end.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std; // Function to check if n is palindromebool isPalindrome(int n){    // Find the appropriate divisor    // to extract the leading digit    int divisor = 1;    while (n / divisor >= 10)        divisor *= 10;     while (n != 0) {        int leading = n / divisor;        int trailing = n % 10;         // If first and last digits are        // not same then return false        if (leading != trailing)            return false;         // Removing the leading and trailing        // digits from the number        n = (n % divisor) / 10;         // Reducing divisor by a factor        // of 2 as 2 digits are dropped        divisor = divisor / 100;    }    return true;} // Function to find the largest palindromic numberint largestPalindrome(int A[], int n){    int currentMax = -1;     for (int i = 0; i < n; i++) {         // If a palindrome larger than the currentMax is found        if (A[i] > currentMax && isPalindrome(A[i]))            currentMax = A[i];    }     // Return the largest palindromic number from the array    return currentMax;} // Driver programint main(){    int A[] = { 1, 232, 54545, 999991 };    int n = sizeof(A) / sizeof(A[0]);     // print required answer    cout << largestPalindrome(A, n);     return 0;}
// Java implementation of above approach import java.util.*; class GFG{    // Function to check if n is palindrome    static boolean isPalindrome(int n)    {        // Find the appropriate divisor        // to extract the leading digit        int divisor = 1;        while (n / divisor >= 10)            divisor *= 10;             while (n != 0) {            int leading = n / divisor;            int trailing = n % 10;                 // If first and last digits are            // not same then return false            if (leading != trailing)                return false;                 // Removing the leading and trailing            // digits from the number            n = (n % divisor) / 10;                 // Reducing divisor by a factor            // of 2 as 2 digits are dropped            divisor = divisor / 100;        }        return true;    }         // Function to find the largest palindromic number    static int largestPalindrome(int []A, int n)    {        int currentMax = -1;             for (int i = 0; i < n; i++) {                 // If a palindrome larger than the currentMax is found            if (A[i] > currentMax && isPalindrome(A[i]))                currentMax = A[i];        }             // Return the largest palindromic number from the array        return currentMax;    }         // Driver program    public static void main(String []args)    {        int []A = { 1, 232, 54545, 999991 };        int n = A.length;             // print required answer        System.out.println(largestPalindrome(A, n));                  } } // This code is contributed// by ihritik
# Python 3 implementation of above approach # Function to check if n is palindromedef isPalindrome(n):         # Find the appropriate divisor    # to extract the leading digit    divisor = 1    while (int(n / divisor) >= 10):        divisor *= 10     while (n != 0):        leading = int(n / divisor)        trailing = n % 10         # If first and last digits are        # not same then return false        if (leading != trailing):            return False         # Removing the leading and trailing        # digits from the number        n = int((n % divisor) / 10)         # Reducing divisor by a factor        # of 2 as 2 digits are dropped        divisor = int(divisor / 100)    return True # Function to find the largest# palindromic numberdef largestPalindrome(A, n):    currentMax = -1     for i in range(0, n, 1):                 # If a palindrome larger than        # the currentMax is found        if (A[i] > currentMax and isPalindrome(A[i])):            currentMax = A[i]         # Return the largest palindromic    # number from the array    return currentMax # Driver Codeif __name__ == '__main__':    A = [1, 232, 54545, 999991]    n = len(A)     # print required answer    print(largestPalindrome(A, n)) # This code is contributed by# Surendra_Gangwar
// C# implementation of above approach using System; class GFG{    // Function to check if n is palindrome    static bool isPalindrome(int n)    {        // Find the appropriate divisor        // to extract the leading digit        int divisor = 1;        while (n / divisor >= 10)            divisor *= 10;             while (n != 0) {            int leading = n / divisor;            int trailing = n % 10;                 // If first and last digits are            // not same then return false            if (leading != trailing)                return false;                 // Removing the leading and trailing            // digits from the number            n = (n % divisor) / 10;                 // Reducing divisor by a factor            // of 2 as 2 digits are dropped            divisor = divisor / 100;        }        return true;    }         // Function to find the largest palindromic number    static int largestPalindrome(int []A, int n)    {        int currentMax = -1;             for (int i = 0; i < n; i++) {                 // If a palindrome larger than the currentMax is found            if (A[i] > currentMax && isPalindrome(A[i]))                currentMax = A[i];        }             // Return the largest palindromic number from the array        return currentMax;    }         // Driver program    public static void Main()    {        int []A = { 1, 232, 54545, 999991 };        int n = A.Length;             // print required answer        Console.WriteLine(largestPalindrome(A, n));                  } } // This code is contributed// by ihritik
<?php// PHP implementation of above approach // Function to check if n is palindromefunction isPalindrome($n){    // Find the appropriate divisor    // to extract the leading digit    $divisor = 1;    while ((int)($n / $divisor) >= 10)        $divisor *= 10;     while ($n != 0)    {        $leading = (int)($n / $divisor);        $trailing = $n % 10;         // If first and last digits are        // not same then return false        if ($leading != $trailing)            return false;         // Removing the leading and trailing        // digits from the number        $n = ($n % $divisor) / 10;         // Reducing divisor by a factor        // of 2 as 2 digits are dropped        $divisor = $divisor / 100;    }    return true;} // Function to find the largest// palindromic numberfunction largestPalindrome($A, $n){    $currentMax = -1;     for ($i = 0; $i < $n; $i++)    {         // If a palindrome larger than        // the currentMax is found        if ($A[$i] > $currentMax &&            isPalindrome($A[$i]))            $currentMax = $A[$i];    }     // Return the largest palindromic    // number from the array    return $currentMax;} // Driver Code$A = array(1, 232, 54545, 999991);$n = sizeof($A); // print required answerecho(largestPalindrome($A, $n)); // This code is contributed// by Mukul Singh?>
<script> // Javascript implementation of above approach // Function to check if n is palindromefunction isPalindrome(n){         // Find the appropriate divisor    // to extract the leading digit    var divisor = 1;    while (parseInt(n / divisor) >= 10)        divisor *= 10;     while (n != 0)    {        var leading = parseInt(n / divisor);        var trailing = n % 10;         // If first and last digits are        // not same then return false        if (leading != trailing)            return false;         // Removing the leading and trailing        // digits from the number        n = parseInt((n % divisor) / 10);         // Reducing divisor by a factor        // of 2 as 2 digits are dropped        divisor = parseInt(divisor / 100);    }    return true;} // Function to find the largest palindromic numberfunction largestPalindrome(A, n){    var currentMax = -1;     for(var i = 0; i < n; i++)    {                 // If a palindrome larger than the        // currentMax is found        if (A[i] > currentMax && isPalindrome(A[i]))            currentMax = A[i];    }     // Return the largest palindromic    // number from the array    return currentMax;} // Driver codevar A = [ 1, 232, 54545, 999991 ];var n = A.length; // Print required answerdocument.write( largestPalindrome(A, n)); // This code is contributed by rrrtnx </script>
54545
 
ihritik
ankthon
SURENDRA_GANGWAR
Akanksha_Rai
Code_Mech
SoumikMondal
rrrtnx
sumitgumber28
array-traversal-question
palindrome
Arrays
Mathematical
Arrays
Mathematical
palindrome
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.