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 of all, create all permutations of given integer and for each permutation check whether the permutation is 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, maximum one digit can have odd frequency and rest digit must have even frequency. Also, a number to be divisible by 3, sum of its digits must be divisible by 3. So, calculate the digit and and store the frequency of digits, by computing the same analysing, the result can easily be concluded.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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.
?>

chevron_right


Output:

True

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



My Personal Notes arrow_drop_up

Discovering ways to develop a plane for soaring career goals

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, jit_t, ChitraNayal