给定一个整数 。任务是检查其任何排列是否是回文并可以被3整除。
例子:
Input : N = 34734
Output : True
Input : N = 34234
Output : False
基本方法:首先,创建给定整数的所有排列,并针对每个排列检查排列是否为回文且是否可以被3整除。创建所有可能的排列将花费大量时间,然后针对每个排列检查是否为回文。为此的时间复杂度为O(n * n!)。
高效的方法:可以看出,对于任何数字而言,回文数最多只能有一个数字出现奇数频率,而其余数字必须出现偶数频率。另外,要被3整除的数字,其位数之和必须被3整除。因此,计算该数字并存储数字的频率,通过计算相同的分析,可以轻松得出结果。
下面是上述方法的实现:
C++
// C++ program to check if any permutation
// of a number is divisible by 3
// and is Palindromic
#include
using namespace std;
// Function to check if any permutation
// of a number is divisible by 3
// and is Palindromic
bool isDivisiblePalindrome(int n)
{
// Hash array to store frequency
// of digits of n
int hash[10] = { 0 };
int digitSum = 0;
// traverse the digits of integer
// and store their frequency
while (n) {
// Calculate the sum of
// digits simultaneously
digitSum += n % 10;
hash[n % 10]++;
n /= 10;
}
// Check if number is not
// divisible by 3
if (digitSum % 3 != 0)
return false;
int oddCount = 0;
for (int i = 0; i < 10; i++) {
if (hash[i] % 2 != 0)
oddCount++;
}
// If more than one digits have odd frequency,
// palindromic permutation not possible
if (oddCount > 1)
return false;
else
return true;
}
// Driver Code
int main()
{
int n = 34734;
isDivisiblePalindrome(n) ?
cout << "True" :
cout << "False";
return 0;
}
Java
// Java implementation of the above approach
public class GFG{
// Function to check if any permutation
// of a number is divisible by 3
// and is Palindromic
static boolean isDivisiblePalindrome(int n)
{
// Hash array to store frequency
// of digits of n
int hash[] = new int[10];
int digitSum = 0;
// traverse the digits of integer
// and store their frequency
while (n != 0) {
// Calculate the sum of
// digits simultaneously
digitSum += n % 10;
hash[n % 10]++;
n /= 10;
}
// Check if number is not
// divisible by 3
if (digitSum % 3 != 0)
return false;
int oddCount = 0;
for (int i = 0; i < 10; i++) {
if (hash[i] % 2 != 0)
oddCount++;
}
// If more than one digits have odd frequency,
// palindromic permutation not possible
if (oddCount > 1)
return false;
else
return true;
}
// Driver Code
public static void main(String []args){
int n = 34734;
System.out.print(isDivisiblePalindrome(n)) ;
}
// This code is contributed by ANKITRAI1
}
Python 3
# Python 3 program to check if
# any permutation of a number
# is divisible by 3 and is Palindromic
# Function to check if any permutation
# of a number is divisible by 3
# and is Palindromic
def isDivisiblePalindrome(n):
# Hash array to store frequency
# of digits of n
hash = [0] * 10
digitSum = 0
# traverse the digits of integer
# and store their frequency
while (n) :
# Calculate the sum of
# digits simultaneously
digitSum += n % 10
hash[n % 10] += 1
n //= 10
# Check if number is not
# divisible by 3
if (digitSum % 3 != 0):
return False
oddCount = 0
for i in range(10) :
if (hash[i] % 2 != 0):
oddCount += 1
# If more than one digits have
# odd frequency, palindromic
# permutation not possible
if (oddCount > 1):
return False
else:
return True
# Driver Code
n = 34734
if (isDivisiblePalindrome(n)):
print("True")
else:
print("False")
# This code is contributed
# by ChitraNayal
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to check if any permutation
// of a number is divisible by 3
// and is Palindromic
static bool isDivisiblePalindrome(int n)
{
// Hash array to store frequency
// of digits of n
int []hash = new int[10];
int digitSum = 0;
// traverse the digits of integer
// and store their frequency
while (n != 0)
{
// Calculate the sum of
// digits simultaneously
digitSum += n % 10;
hash[n % 10]++;
n /= 10;
}
// Check if number is not
// divisible by 3
if (digitSum % 3 != 0)
return false;
int oddCount = 0;
for (int i = 0; i < 10; i++)
{
if (hash[i] % 2 != 0)
oddCount++;
}
// If more than one digits have odd frequency,
// palindromic permutation not possible
if (oddCount > 1)
return false;
else
return true;
}
// Driver Code
static public void Main ()
{
int n = 34734;
Console.WriteLine(isDivisiblePalindrome(n));
}
}
// This code is contributed by ajit
PHP
1)
return true;
else
return false;
}
// Driver Code
$n = 34734;
if(isDivisiblePalindrome($n))
echo "True" ;
else
echo "False";
# This Code is contributed by Tushill.
?>
输出:
True
时间复杂度:O(n),其中n是给定数字中的位数。