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 swaps required to make a number divisible by 60
- Minimum number of moves required to reach the destination by the king in a chess board
- Count minimum moves required to convert A to B
- Minimum operations required to make all Array elements divisible by K
- Minimum swaps required to make a binary string divisible by 2^k
- Number of moves required to guess a permutation.
- Minimum number of changes required to make the given array an AP
- Minimum number of moves to reach N starting from (1, 1)
- Count minimum number of moves to front or end to sort an array
- Minimum operations of the given type required to make a complete graph
- Minimum number of cuts required to make circle segments equal sized
- Find the minimum number of operations required to make all array elements equal
- Count of minimum reductions required to get the required sum K
- Find minimum moves to reach target on an infinite line
- Minimum time to reach a point with +t and -t moves at time t
- Minimum moves taken to move coin of each cell to any one cell of Matrix
- Find minimum moves to bring all elements in one cell of a matrix
- Minimum moves to reach target on a infinite line | Set 2
- Number of hours after which the second person moves ahead of the first person if they travel at a given speed
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.