Check if any permutation of a number is divisible by 3 and is Palindromic
Last Updated :
05 Sep, 2022
Given an integer N. The task is to check whether any of its permutations is a palindrome and divisible by 3 or not.
Examples:
Input : N = 34734
Output : True
Input : N = 34234
Output : False
Basic Approach: First, create all permutations of a given integer and for each permutation check whether the permutation is a palindrome and divisible by 3 as well. This will take a lot of time to create all possible permutations and then for each permutation check whether it is palindrome or not. The time complexity for this is O(n*n!).
Efficient Approach: It can be observed that for any number to be a palindrome, a maximum of one digit can have an odd frequency and the rest digit must have an even frequency. Also, for a number to be divisible by 3, the sum of its digits must be divisible by 3. So, calculate the digit and store the frequency of digits, by computing the same analysis, the result can easily be concluded.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isDivisiblePalindrome( int n)
{
int hash[10] = { 0 };
int digitSum = 0;
while (n) {
digitSum += n % 10;
hash[n % 10]++;
n /= 10;
}
if (digitSum % 3 != 0)
return false ;
int oddCount = 0;
for ( int i = 0; i < 10; i++) {
if (hash[i] % 2 != 0)
oddCount++;
}
if (oddCount > 1)
return false ;
else
return true ;
}
int main()
{
int n = 34734;
isDivisiblePalindrome(n) ?
cout << "True" :
cout << "False" ;
return 0;
}
|
Java
public class GFG{
static boolean isDivisiblePalindrome( int n)
{
int hash[] = new int [ 10 ];
int digitSum = 0 ;
while (n != 0 ) {
digitSum += n % 10 ;
hash[n % 10 ]++;
n /= 10 ;
}
if (digitSum % 3 != 0 )
return false ;
int oddCount = 0 ;
for ( int i = 0 ; i < 10 ; i++) {
if (hash[i] % 2 != 0 )
oddCount++;
}
if (oddCount > 1 )
return false ;
else
return true ;
}
public static void main(String []args){
int n = 34734 ;
System.out.print(isDivisiblePalindrome(n)) ;
}
}
|
Python 3
def isDivisiblePalindrome(n):
hash = [ 0 ] * 10
digitSum = 0
while (n) :
digitSum + = n % 10
hash [n % 10 ] + = 1
n / / = 10
if (digitSum % 3 ! = 0 ):
return False
oddCount = 0
for i in range ( 10 ) :
if ( hash [i] % 2 ! = 0 ):
oddCount + = 1
if (oddCount > 1 ):
return False
else :
return True
n = 34734
if (isDivisiblePalindrome(n)):
print ( "True" )
else :
print ( "False" )
|
C#
using System;
class GFG
{
static bool isDivisiblePalindrome( int n)
{
int []hash = new int [10];
int digitSum = 0;
while (n != 0)
{
digitSum += n % 10;
hash[n % 10]++;
n /= 10;
}
if (digitSum % 3 != 0)
return false ;
int oddCount = 0;
for ( int i = 0; i < 10; i++)
{
if (hash[i] % 2 != 0)
oddCount++;
}
if (oddCount > 1)
return false ;
else
return true ;
}
static public void Main ()
{
int n = 34734;
Console.WriteLine(isDivisiblePalindrome(n));
}
}
|
PHP
<?php
function isDivisiblePalindrome( $n )
{
$hash = array (0 );
$digitSum = 0;
while ( $n ) {
$digitSum += $n % 10;
$hash ++;
$n /= 10;
}
if ( $digitSum % 3 != 0)
return false;
$oddCount = 0;
for ( $i = 0; $i < 10; $i ++)
{
if ( $hash % 2 != 0)
$oddCount ++;
}
if ( $oddCount > 1)
return true;
else
return false;
}
$n = 34734;
if (isDivisiblePalindrome( $n ))
echo "True" ;
else
echo "False" ;
# This Code is contributed by Tushill.
?>
|
Javascript
<script>
function isDivisiblePalindrome(n)
{
var hash = Array(10).fill(0);
var digitSum = 0;
while (n) {
digitSum += n % 10;
hash[n % 10]++;
n = parseInt(n/10);
}
if (digitSum % 3 != 0)
return false ;
var oddCount = 0;
for ( var i = 0; i < 10; i++) {
if (hash[i] % 2 != 0)
oddCount++;
}
if (oddCount > 1)
return false ;
else
return true ;
}
var n = 34734;
isDivisiblePalindrome(n) ?
document.write( "True" ):
document.write( "False" );
</script>
|
Time Complexity: O(logn), where n is the number of digits in the given number.
Auxiliary Space: O(10)
Share your thoughts in the comments
Please Login to comment...