 Open in App
Not now

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

• Difficulty Level : Easy
• 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++

 `// 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 = { 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``;` `    ``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.``?>`

## Javascript

 ``

Output:

`True`

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

Auxiliary Space: O(10)

My Personal Notes arrow_drop_up