Given an integer . The task is to check whether any of its permutations is a palindrome and divisible by 3 or not.
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:
Time Complexity: O(n), where n is the number of digits in the given number.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if any permutation of a number without any leading zeros is a power of 2 or not
- Check if any permutation of a large number is divisible by 8
- Check if any permutation of N equals any power of K
- Check if any permutation of array contains sum of every adjacent pair not divisible by 3
- Smallest non-zero substring which has any permutation divisible by 2^K
- Find permutation of n which is divisible by 3 but not divisible by 6
- Check if K can be obtained by performing arithmetic operations on any permutation of an Array
- Print the last character of lexicographically smallest non-palindromic permutation of a string
- Check if any large number is divisible by 19 or not
- Check if any large number is divisible by 17 or not
- Program to check if a number is divisible by any of its digits
- Sum of all N-digit palindromic numbers which doesn't contains 0 and are divisible by 9
- Maximize Bitwise AND of first element with complement of remaining elements for any permutation of given Array
- Check if there is any pair in a given range with GCD is divisible by k
- Check if the sum of primes is divisible by any prime from the array
- Check if any valid sequence is divisible by M
- Count of N digit palindromic numbers divisible by 9
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- Maximize minimum distance between repetitions from any permutation of the given Array
- Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.