Given an integer . The task is to find another interger which is permutation of n, divisible by 3 but not divisible by 6. Given that n is divisible by 6. If no such permutation is possible print -1.
Input: n = 336 Output: 363 Input: n = 48 Output: -1
For a number to be divisible by 6, it must be divisible by 3 as well as 2, means every even integer divisible by 3 is divisible by 6. So, an integer which is divisible by 3 but not 6 is odd integer divisible by 3.
So, if integer n contains any odd integer then there exists a permutation which is divisible by 3 but not 6, else no such permutation exist.
- let LEN is length of integer (i.e. ceil(log10(n))).
- iterate over LEN and check whether n is even or odd.
- if n is odd return n
- else right – rotate n once. and continue.
- if LEN is over return -1
Below is the implementation of the above approach:
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.
- Count numbers in range 1 to N which are divisible by X but not by Y
- Count natural numbers whose factorials are divisible by x but not y
- Check if roots of a Quadratic Equation are numerically equal but opposite in sign or not
- Find the largest composite number that divides N but is strictly lesser than N
- Smallest non-zero substring which has any permutation divisible by 2^K
- Check if any permutation of array contains sum of every adjacent pair not divisible by 3
- Permutation of Array such that sum of adjacent elements are not divisible by 3
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Percentage increase in the cylinder if the height is increased by given percentage but radius remains constant
- Find the Nth natural number which is not divisible by A
- Find the Kth number which is not divisible by N
- Find a permutation such that number of indices for which gcd(p[i], i) > 1 is exactly K
- Split a number as sum of K numbers which are not divisible by K
- Sum of first K numbers which are not divisible by N
- Check if there exists a permutation of given string which doesn't contain any monotonous substring
- Count of subarrays of size K which is a permutation of numbers from 1 to K
- Count of subarrays which forms a permutation from given Array elements
- Check if any permutation of a large number is divisible by 8
- Check if any permutation of a number is divisible by 3 and is Palindromic
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.