# Minimum number of given moves required to make N divisible by 25

Given a number **N**(1 ≤ N ≤ 10^{18}) 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**.

**Examples:**

Input:N = 7560

Output:1

swap(5, 6) and N becomes 7650 which is divisible by 25

Input:N = 100

Output:0

**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:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the minimum number ` `// of moves required to make n divisible ` `// by 25 ` `int` `minMoves(` `long` `long` `n) ` `{ ` ` ` ` ` `// Convert number into string ` ` ` `string s = to_string(n); ` ` ` ` ` `// To store required answer ` ` ` `int` `ans = INT_MAX; ` ` ` ` ` `// Length of the string ` ` ` `int` `len = s.size(); ` ` ` ` ` `// To check all possible pairs ` ` ` `for` `(` `int` `i = 0; i < len; ++i) { ` ` ` `for` `(` `int` `j = 0; j < len; ++j) { ` ` ` `if` `(i == j) ` ` ` `continue` `; ` ` ` ` ` `// Make a duplicate string ` ` ` `string t = s; ` ` ` `int` `cur = 0; ` ` ` ` ` `// Number of swaps required to place ` ` ` `// ith digit in last position ` ` ` `for` `(` `int` `k = i; k < len - 1; ++k) { ` ` ` `swap(t[k], t[k + 1]); ` ` ` `++cur; ` ` ` `} ` ` ` ` ` `// Number of swaps required to place ` ` ` `// jth digit in 2nd last position ` ` ` `for` `(` `int` `k = j - (j > i); k < len - 2; ++k) { ` ` ` `swap(t[k], t[k + 1]); ` ` ` `++cur; ` ` ` `} ` ` ` ` ` `// Find first non zero digit ` ` ` `int` `pos = -1; ` ` ` `for` `(` `int` `k = 0; k < len; ++k) { ` ` ` `if` `(t[k] != ` `'0'` `) { ` ` ` `pos = k; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Place first non zero digit ` ` ` `// in the first position ` ` ` `for` `(` `int` `k = pos; k > 0; --k) { ` ` ` `swap(t[k], t[k - 1]); ` ` ` `++cur; ` ` ` `} ` ` ` ` ` `// Convert string to number ` ` ` `long` `long` `nn = atoll(t.c_str()); ` ` ` ` ` `// If this number is divisible by 25 ` ` ` `// then cur is one of the possible answer ` ` ` `if` `(nn % 25 == 0) ` ` ` `ans = min(ans, cur); ` ` ` `} ` ` ` `} ` ` ` ` ` `// If not possible ` ` ` `if` `(ans == INT_MAX) ` ` ` `return` `-1; ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `long` `long` `n = 509201; ` ` ` `cout << minMoves(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

4

## Recommended Posts:

- 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 array insertions required to make consecutive difference <= K
- Minimum operations of the given type required to make a complete graph
- Minimum operations required to make all the array elements equal
- Minimum operations required to make all the elements distinct in an array
- Minimum flip required to make Binary Matrix symmetric
- 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
- Possible to make a divisible by 3 number using all digits in an array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.