Open In App

Check if any permutation of a number is divisible by 3 and is Palindromic

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++ program to check if any permutation
// of a number is divisible by 3
// and is Palindromic
 
#include <bits/stdc++.h>
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 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 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# 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
// PHP 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
 
function isDivisiblePalindrome($n)
{
    // Hash array to store frequency
    // of digits of n
    $hash = array(0 );
 
    $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;
    }
 
    // Check if number is not
    // divisible by 3
    if ($digitSum % 3 != 0)
        return false;
 
    $oddCount = 0;
    for ($i = 0; $i < 10; $i++)
    {
        if ($hash % 2 != 0)
            $oddCount++;
    }
 
    // If more than one digits have odd frequency,
    // palindromic permutation not possible
    if ($oddCount > 1)
        return true;
    else
        return false;
}
 
// Driver Code
    $n = 34734;
 
    if(isDivisiblePalindrome($n))
            echo "True" ;
            else
            echo "False";
 
# This Code is contributed by Tushill.
?>




<script>
 
// Javascript 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
function isDivisiblePalindrome(n)
{
    // Hash array to store frequency
    // of digits of n
    var hash = Array(10).fill(0);
 
    var 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 = parseInt(n/10);
    }
 
    // Check if number is not
    // divisible by 3
    if (digitSum % 3 != 0)
        return false;
 
    var oddCount = 0;
    for (var 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
var n = 34734;
isDivisiblePalindrome(n) ?
         document.write( "True" ):
              document.write( "False");
 
 
</script>

Output: 
True

 

Time Complexity: O(logn), where n is the number of digits in the given number.

Auxiliary Space: O(10)
 


Article Tags :