Check if any permutation of a number is divisible by 3 and is Palindromic
Given an integer
. 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++
// 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
// 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
<?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. ?> |
True
Time Complexity: O(n), where n is the number of digits in the given number.
Recommended Posts:
- Check if any permutation of a large number is divisible by 8
- Find permutation of n which is divisible by 3 but not divisible by 6
- Check if a number is divisible by 41 or not
- Check if a number is divisible by 23 or not
- Check if a number is divisible by all prime divisors of another number
- Check if a large number is divisible by 4 or not
- Check if a large number is divisible by 9 or not
- Check if a large number is divisible by 6 or not
- Check if a large number is divisible by 8 or not
- Check if a larger number divisible by 36
- Check if a large number is divisible by 5 or not
- Check if any large number is divisible by 17 or not
- Check if a large number is divisible by 13 or not
- Check if any large number is divisible by 19 or not
- Check if a large number is divisible by 3 or not
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.



