Minimum number of swaps required to make a number divisible by 60 Last Updated : 02 Feb, 2022 Comments Improve Suggest changes Like Article Like Report 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".Examples: Input: N = 603 Output: 2 Explanation: 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 Output: -1 Approach: 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, orIf there is no digits which is divisible by 2, orIf the number does not contain any 0,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 CPP // C++ program to find minimum number // of swap operations required #include <bits/stdc++.h> using namespace std; // Function that print minimum number // of swap operations required void MinimumSwapOperations(string s) { bool zero_exist = false; bool multiple_of_2 = false; int sum = 0; int index_of_zero; bool more_zero = false; for (int i = 0; i < s.length(); i++) { int val = s[i] - '0'; // Condition if more than one // zero exist if (zero_exist == true) more_zero = true; // Condition if zero_exist if (val == 0) { zero_exist = true; index_of_zero = i; } // Computing total sum of all digits sum += val; } // Condition if zero does not exist or // the sum is not divisible by 3 if (zero_exist == false || sum % 3 != 0) { cout << "-1" << "\n"; return; } for (int i = 0; i < s.length(); i++) { int val = s[i] - '0'; // Condition to find a digit that is // multiple of 2 other than one zero if (val % 2 == 0 && i != index_of_zero) multiple_of_2 = true; } // Condition if multiple of 2 // do not exist if (multiple_of_2 == false) { cout << "-1" << "\n"; return; } int last_val = s[s.length() - 1] - '0'; int second_last_val = s[s.length() - 2] - '0'; // Condition for zero swaps // means the number is already // is divisible by 60 if (last_val == 0 && second_last_val % 2 == 0) cout << 0 << "\n"; // Condition for only one swap else if ((last_val == 0 && second_last_val % 2 != 0) || (last_val % 2 == 0 && second_last_val == 0)) cout << 1 << "\n"; else if (more_zero == true && (last_val == 0 && second_last_val % 2 != 0)) cout << 1 << "\n"; // Otherwise 2 swaps required else cout << 2 << "\n"; } // Driver Code int main() { string N = "20"; MinimumSwapOperations(N); return 0; } Java // Java program to find minimum number // of swap operations required class GFG { // Function that print minimum number // of swap operations required static void MinimumSwapOperations(String s) { boolean zero_exist = false; boolean multiple_of_2 = false; int sum = 0; int index_of_zero = 0; boolean more_zero = false; for (int i = 0; i < s.length(); i++) { int val = s.charAt(i) - '0'; // Condition if more than one // zero exist if (zero_exist == true) more_zero = true; // Condition if zero_exist if (val == 0) { zero_exist = true; index_of_zero = i; } // Computing total sum of all digits sum += val; } // Condition if zero does not exist or // the sum is not divisible by 3 if (zero_exist == false || sum % 3 != 0) { System.out.println("-1"); return; } for (int i = 0; i < s.length(); i++) { int val = s.charAt(i) - '0'; // Condition to find a digit that is // multiple of 2 other than one zero if (val % 2 == 0 && i != index_of_zero) multiple_of_2 = true; } // Condition if multiple of 2 // do not exist if (multiple_of_2 == false) { System.out.println("-1"); return; } int last_val = s.charAt(s.length() - 1)- '0'; int second_last_val = s.charAt(s.length() - 2)- '0'; // Condition for zero swaps // means the number is already // is divisible by 60 if (last_val == 0&& second_last_val % 2 == 0) System.out.println(0); // Condition for only one swap else if ((last_val == 0 && second_last_val % 2 != 0) || (last_val % 2 == 0 && second_last_val == 0)) System.out.println(1); else if (more_zero == true && (last_val == 0 && second_last_val % 2 != 0)) System.out.println(1) ; // Otherwise 2 swaps required else System.out.println(2) ; } // Driver Code public static void main (String[] args) { String N = "20"; MinimumSwapOperations(N); } } // This code is contributed by AnkitRai01 Python3 # Python3 program to find minimum number # of swap operations required # Function that print minimum number # of swap operations required def MinimumSwapOperations(s): zero_exist = False multiple_of_2 = False sum = 0 index_of_zero = 0 more_zero = False for i in range(len(s)): val = ord(s[i]) - ord('0') # Condition if more than one # zero exist if (zero_exist == True): more_zero = True # Condition if zero_exist if (val == 0): zero_exist = True index_of_zero = i # Computing total sum of all digits sum += val # Condition if zero does not exist or # the sum is not divisible by 3 if (zero_exist == False or sum % 3 != 0): print("-1") return for i in range(len(s)): val = ord(s[i]) - ord('0') # Condition to find a digit that is # multiple of 2 other than one zero if (val % 2 == 0 and i != index_of_zero): multiple_of_2 = True # Condition if multiple of 2 # do not exist if (multiple_of_2 == False): print("-1") return last_val = ord(s[len(s) - 1]) - ord('0') second_last_val = ord(s[len(s) - 2])- ord('0') # Condition for zero swaps # means the number is already # is divisible by 60 if (last_val == 0 and second_last_val % 2 == 0): print(0) # Condition for only one swap elif ((last_val == 0 and second_last_val % 2 != 0) or (last_val % 2 == 0 and second_last_val == 0)): print(1) elif (more_zero == True and (last_val == 0 and second_last_val % 2 != 0)): print(1) # Otherwise 2 swaps required else: print(2) # Driver Code if __name__ == '__main__': N = "20" MinimumSwapOperations(N) # This code is contributed by mohit kumar 29 C# // C# program to find minimum number // of swap operations required using System; class GFG { // Function that print minimum number // of swap operations required static void MinimumSwapOperations(string s) { bool zero_exist = false; bool multiple_of_2 = false; int sum = 0; int index_of_zero = 0; bool more_zero = false; for (int i = 0; i < s.Length; i++) { int val = s[i] - '0'; // Condition if more than one // zero exist if (zero_exist == true) more_zero = true; // Condition if zero_exist if (val == 0) { zero_exist = true; index_of_zero = i; } // Computing total sum of all digits sum += val; } // Condition if zero does not exist or // the sum is not divisible by 3 if (zero_exist == false || sum % 3 != 0) { Console.WriteLine("-1"); return; } for (int i = 0; i < s.Length; i++) { int val = s[i] - '0'; // Condition to find a digit that is // multiple of 2 other than one zero if (val % 2 == 0 && i != index_of_zero) multiple_of_2 = true; } // Condition if multiple of 2 // do not exist if (multiple_of_2 == false) { Console.WriteLine("-1"); return; } int last_val = s[(s.Length - 1)- '0']; int second_last_val = s[(s.Length - 2)- '0']; // Condition for zero swaps // means the number is already // is divisible by 60 if (last_val == 0&& second_last_val % 2 == 0) Console.WriteLine(0); // Condition for only one swap else if ((last_val == 0 && second_last_val % 2 != 0) || (last_val % 2 == 0 && second_last_val == 0)) Console.WriteLine(1); else if (more_zero == true && (last_val == 0 && second_last_val % 2 != 0)) Console.WriteLine(1) ; // Otherwise 2 swaps required else Console.WriteLine(2) ; } // Driver Code public static void Main (string[] args) { string N = "20"; MinimumSwapOperations(N); } } // This code is contributed by AnkitRai01 JavaScript <script> // Javascript program to find minimum number // of swap operations required // Function that print minimum number // of swap operations required function MinimumSwapOperations(s) { let zero_exist = false; let multiple_of_2 = false; let sum = 0; let index_of_zero = 0; let more_zero = false; for (let i = 0; i < s.length; i++) { let val = s[i] - '0'; // Condition if more than one // zero exist if (zero_exist == true) more_zero = true; // Condition if zero_exist if (val == 0) { zero_exist = true; index_of_zero = i; } // Computing total sum of all digits sum += val; } // Condition if zero does not exist or // the sum is not divisible by 3 if (zero_exist == false || sum % 3 != 0) { document.write("-1"); return; } for (let i = 0; i < s.length; i++) { let val = s[i] - '0'; // Condition to find a digit that is // multiple of 2 other than one zero if (val % 2 == 0 && i != index_of_zero) multiple_of_2 = true; } // Condition if multiple of 2 // do not exist if (multiple_of_2 == false) { document.write("-1"); return; } let last_val = s.charAt[s.length - 1] - '0'; let second_last_val = s[(s.length - 2)]- '0'; // Condition for zero swaps // means the number is already // is divisible by 60 if (last_val == 0&& second_last_val % 2 == 0) document.write(0); // Condition for only one swap else if ((last_val == 0 && second_last_val % 2 != 0) || (last_val % 2 == 0 && second_last_val == 0)) document.write(1); else if (more_zero == true && (last_val == 0 && second_last_val % 2 != 0)) document.write(1) ; // Otherwise 2 swaps required else document.write(2) ; } // Driver Code let N = "20"; MinimumSwapOperations(N); </script> Output: -1 Time complexity: O(N) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Minimum swaps required to make a binary string divisible by 2^k N nitinkr8991 Follow Improve Article Tags : Mathematical DSA divisibility Number Divisibility Practice Tags : Mathematical Similar Reads Minimum number of given moves required to make N divisible by 25 Given a number N(1 ? N ? 1018) without leading zeros. The task is to find the minimum number of moves required to make N divisible by 25. At each move, one can swap any two adjacent digits and make sure that at any time number must not contain any leading zeros. If it is not possible to make N divis 11 min read Minimum number of swaps required to sort an array of first N number Given an array arr[] of distinct integers from 1 to N. The task is to find the minimum number of swaps required to sort the array. Example: Input: arr[] = { 7, 1, 3, 2, 4, 5, 6 } Output: 5 Explanation: i arr swap (indices) 0 [7, 1, 3, 2, 4, 5, 6] swap (0, 3) 1 [2, 1, 3, 7, 4, 5, 6] swap (0, 1) 2 [1, 5 min read Minimum swaps required to make a binary string divisible by 2^k Given a binary string S of length N and an integer K, the task is to find the minimum number of adjacent swaps required to make the binary string divisible by 2K. If it is not possible then print -1. Examples: Input: S = "100111", K = 2 Output: 6 Swapping the right-most zero 3 times to the right, we 6 min read Minimum and maximum number of digits required to be removed to make a given number divisible by 3 Given a numeric string S, the task is to find the minimum and the maximum number of digits that must be removed from S such that it is divisible by 3. If it is impossible to do so, then print "-1". Examples: Input: S = "12345" Output: Minimum: 0 Maximum: 4 Explanation: The given number 12345 is divi 11 min read Minimum operations required to make product divisible by k Given an array arr[] consisting of positive integers and an integer K, the task is to find the minimum number of operations required to make the product of all elements of the array divisible by K where in one operation you can select any prime number and insert it into the array. Examples: Input: a 11 min read Smallest number required to be added to M to make it divisible by N Given two positive integers M and N, the task is to calculate the smallest number that needs to be added to M, to make it divisible by N. Examples: Input: M = 6, N = 7Output: 1Explanation: 1 is the smallest number that can be added to 6 to make it divisible by 7. Input: M = 100, N = 28Output: 12 App 5 min read Like