📜  数组中最大回文数

📅  最后修改于: 2021-04-24 03:19:17             🧑  作者: Mango

给定非负整数arr []的数组。任务是找到阵列中最大的回文数。如果没有这样的数字,则打印-1

例子:

方法1:

  • 以升序对数组进行排序。
  • 从头开始遍历数组。
  • 第一个数字是回文数是必需的答案。
  • 如果找不到回文数,则打印-1

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Function to check if n is palindrome
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
int 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 program
int main()
{
    int A[] = { 1, 232, 54545, 999991 };
    int n = sizeof(A) / sizeof(A[0]);
  
    // print required answer
    cout << largestPalindrome(A, n);
  
    return 0;
}


Java
// 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
# Python3 implementation of above approach 
  
# Function to check if n is palindrome 
def 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 number 
def 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 Code
if __name__ == "__main__" : 
  
    A = [ 1, 232, 54545, 999991 ] 
    n = len(A)
  
    # print required answer 
    print(largestPalindrome(A, n))
  
# This code is contributed by Ryuga


C#
// 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
= 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 number
function 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 answer
echo largestPalindrome($A, $n);
  
// This code is contributed 
// by Akanksha Rai
?>


C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Function to check if n is palindrome
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
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
int main()
{
    int A[] = { 1, 232, 54545, 999991 };
    int n = sizeof(A) / sizeof(A[0]);
  
    // print required answer
    cout << largestPalindrome(A, n);
  
    return 0;
}


Java
// 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


Python3
# Python 3 implementation of above approach
  
# Function to check if n is palindrome
def 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 number
def 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 Code
if __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#
// 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
= 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 number
function 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 answer
echo(largestPalindrome($A, $n));
  
// This code is contributed 
// by Mukul Singh
?>


输出:
54545

方法2:

  • 设置变量currentMax = -1并开始遍历数组。
  • 如果当前元素arr [i]> currentMax并且arr [i]是回文。
  • 然后设置currentMax = arr [i]
  • 最后打印currentMax

下面是上述方法的实现:

C++

// C++ implementation of above approach
#include 
using namespace std;
  
// Function to check if n is palindrome
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
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
int main()
{
    int A[] = { 1, 232, 54545, 999991 };
    int n = sizeof(A) / sizeof(A[0]);
  
    // print required answer
    cout << largestPalindrome(A, n);
  
    return 0;
}

Java

// 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

Python3

# Python 3 implementation of above approach
  
# Function to check if n is palindrome
def 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 number
def 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 Code
if __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#

// 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

= 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 number
function 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 answer
echo(largestPalindrome($A, $n));
  
// This code is contributed 
// by Mukul Singh
?>
输出:
54545