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 divisible by 25 then print -1.


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)
            // 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]);
            // 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]);
            // Find first non zero digit
            int pos = -1;
            for (int k = 0; k < len; ++k) {
                if (t[k] != '0') {
                    pos = k;
            // Place first non zero digit
            // in the first position
            for (int k = pos; k > 0; --k) {
                swap(t[k], t[k - 1]);
            // 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;




My Personal Notes arrow_drop_up


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