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:
- 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
- Find nth number that contains the digit k or divisible by k.
- Find N digits number which is divisible by D
- Find the number closest to n and divisible by m
- Find the first natural number whose factorial is divisible by x
- Find the minimum positive integer such that it is divisible by A and sum of its digits is equal to B
- Find if a number is divisible by every number in a list
- Find set of m-elements with difference of any two elements is divisible by k
- Number is divisible by 29 or not
- Sum of numbers from 1 to N which are divisible by 3 or 4
- Sum of all numbers divisible by 6 in a given range
- Rearrangement of a number which is also divisible by it
- Sum of first N natural numbers which are divisible by X or Y
- Add N digits to A such that it is divisible by B after each addition
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.