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 divisible by 25 then print -1.
Input: N = 7560
swap(5, 6) and N becomes 7650 which is divisible by 25
Input: N = 100
Approach: Iterate over all pairs of digits in the number. Let the first digit in the pair is at position i and the second is at position j. Let’s place these digits to the last two positions in the number. But, now the number can contain a leading zero. Find the leftmost non-zero digit and move it to the first position. Then if the current number is divisible by 25 try to update the answer with the number of swaps. The minimum number of swaps across all of these operations is the required answer.
Below is the implementation of the above approach:
- Find the minimum number of preprocess moves required to make two strings equal
- Minimum number of moves required to reach the destination by the king in a chess board
- Minimum number of changes required to make the given array an AP
- Minimum number of cuts required to make circle segments equal sized
- Number of moves required to guess a permutation.
- Minimum changes required to make two arrays identical
- Minimum operations of the given type required to make a complete graph
- Minimum flip required to make Binary Matrix symmetric
- Minimum operations required to make all the elements distinct in an array
- Minimum operations required to make all the array elements equal
- Minimum array insertions required to make consecutive difference <= K
- Minimum steps to make all the elements of the array divisible by 4
- Minimum pair sum operations to make array each element divisible by 4
- Find the number of operations required to make all array elements Equal
- Number of digits to be removed to make a number divisible by 3
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.