Minimum Rotations to Unlock a Circular Lock

Last Updated : 14 Jul, 2026

Given two positive integers r and d of the same length, representing the current and desired lock configurations, respectively, where each digit corresponds to a circular ring numbered from 0 to 9, find the minimum number of rotations required to transform r into d.

  • In one operation, a ring can be rotated by one position either clockwise or anticlockwise.
  • The rings are circular, so 9 wraps to 0 and 0 wraps to 9.

Examples:  

Input: r = 222, d = 333
Output: 3
Explanation: Each digit 2 can be changed to 3 in one rotation. Therefore, the minimum total rotations required are 1 + 1 + 1 = 3.

Input: r = 2345, d = 5432
Output: 8
Explanation: The minimum rotations required for the corresponding digit pairs (2, 5), (3, 4), (4, 3), and (5, 2) are 3, 1, 1, and 3, respectively. Therefore, the minimum total rotations required are 3 + 1 + 1 + 3 = 8.

Try It Yourself
redirect icon

[Naive Approach] Digit by Digit Simulation - O(n * 10) Time and O(n) Space

The idea is to process each corresponding digit of r and d independently. For every pair of digits, simulate both clockwise and anticlockwise rotations one step at a time until the target digit is reached. Add the smaller number of rotations for each digit to the final answer.

Working of Approach:

  • Traverse each corresponding digit of r and d, and simulate both clockwise and anticlockwise rotations until the target digit is reached.
  • Count the rotations in both directions, choose the smaller count, and add it to the answer.
  • Repeat this process for all digits and return the total minimum rotations.
C++
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int rotationCount(int r, int d)
{

    // Convert numbers into strings.
    string s1 = to_string(r);
    string s2 = to_string(d);

    // Pad the shorter string with leading zeros.
    while (s1.size() < s2.size())
        s1 = "0" + s1;

    while (s2.size() < s1.size())
        s2 = "0" + s2;

    int ans = 0;

    // Process every digit.
    for (int i = 0; i < s1.size(); i++)
    {

        int a = s1[i] - '0';
        int b = s2[i] - '0';

        // Simulate clockwise rotation.
        int cw = 0;
        int cur = a;
        while (cur != b)
        {
            cur = (cur + 1) % 10;
            cw++;
        }

        // Simulate anticlockwise rotation.
        int ccw = 0;
        cur = a;
        while (cur != b)
        {
            cur = (cur + 9) % 10;
            ccw++;
        }

        ans += min(cw, ccw);
    }

    return ans;
}

int main()
{
    int r = 2345, d = 5432;

    cout << rotationCount(r, d);

    return 0;
}
Java
import java.util.*;

class GFG {

    static int rotationCount(int r, int d)
    {

        // Convert numbers into strings.
        String s1 = Integer.toString(r);
        String s2 = Integer.toString(d);

        // Pad the shorter string with leading zeros.
        while (s1.length() < s2.length())
            s1 = "0" + s1;

        while (s2.length() < s1.length())
            s2 = "0" + s2;

        int ans = 0;

        // Process every digit.
        for (int i = 0; i < s1.length(); i++) {

            int a = s1.charAt(i) - '0';
            int b = s2.charAt(i) - '0';

            // Simulate clockwise rotation.
            int cw = 0;
            int cur = a;
            while (cur != b) {
                cur = (cur + 1) % 10;
                cw++;
            }

            // Simulate anticlockwise rotation.
            int ccw = 0;
            cur = a;
            while (cur != b) {
                cur = (cur + 9) % 10;
                ccw++;
            }

            ans += Math.min(cw, ccw);
        }

        return ans;
    }

    public static void main(String[] args)
    {

        int r = 2345, d = 5432;

        System.out.println(rotationCount(r, d));
    }
}
Python
def rotationCount(r, d):

    # Convert numbers into strings.
    s1 = str(r