Given an integer N , the task is to find the minimum number of swaps required to make N divisible by 60. If not possible, then print “-1“.
Input: N = 603
Two swap operations are required:
In the first swap (0, 3): 630
In the second swap (6, 3): 360
Now 360 is divisible by 60.
Therefore the minimum two swaps are required.
Input: N = 205
- For a number to be divisible by 60, it must be divisible by 2, 3 and 10. Therefore:
- If the sum of digits of number is not divisible by 3, or
- If there is no digits which is divisible by 2, or
- If the number does not contain any 0,
No swap can make the number divisible by 60. Hence -1 swaps will be required in this case.
- Then the minimum number of swaps required can be determined by following rules:
- If the number is already is divisible by 60, then 0 swaps are required. This can be determined if last digit (LSB) is 0 and the second last digit is divisible by 2.
- If either of the below cases is true, then 1 swap is required.
- If last digit (LSB) is 0 and the second last digit is not divisible by 2, or, last digit (LSB) is divisible by 2 and the second last digit is 0.
- If last digit (LSB) is 0 and the second last digit is not divisible by 2, and there are more than 1 zeroes present in the number.
- Otherwise 2 swaps required
Below is the implementation of the above approach
Time complexity: O(N)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Minimum swaps required to make a binary string divisible by 2^k
- Minimum number of given moves required to make N divisible by 25
- Minimum number of changes required to make the given array an AP
- Minimum operations required to make all Array elements divisible by K
- Minimum number of cuts required to make circle segments equal sized
- Find the minimum number of operations required to make all array elements equal
- Number of digits to be removed to make a number divisible by 3
- Minimum number of given powers of 2 required to represent a number
- Find minimum number to be divided to make a number a perfect square
- Find the minimum number to be added to N to make it a prime number
- Minimum divisor of a number to make the number perfect cube
- Possible to make a divisible by 3 number using all digits in an array
- Print digit's position to be removed to make a number divisible by 6
- Minimum number of palindromes required to express N as a sum | Set 1
- Minimum number of given operation required to convert n to m
- Minimum number of operations required to reduce N to 1
- Minimum number operations required to convert n to m | Set-2
- Minimum number of palindromes required to express N as a sum | Set 2
- Minimum number of primes required such that their sum is equal to N
- Minimum number of mails required to distribute all the questions
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.