In one operation, you can do one of the four following operations:
Divide x by 11 if x is a multiple of 11.
Divide x by 5 if x is a multiple of 5.
Decrement x by 1.
Increment x by 1.
Return the minimum number of operations required to make xandy equal.
Example 1:
Input: x = 26, y = 1
Output: 3
Explanation: We can make 26 equal to 1 by applying the following operations:
1. Decrement x by 1
2. Divide x by 5
3. Divide x by 5
It can be shown that 3 is the minimum number of operations required to make 26 equal to 1.
Example 2:
Input: x = 54, y = 2
Output: 4
Explanation: We can make 54 equal to 2 by applying the following operations:
1. Increment x by 1
2. Divide x by 11
3. Divide x by 5
4. Increment x by 1
It can be shown that 4 is the minimum number of operations required to make 54 equal to 2.
Example 3:
Input: x = 25, y = 30
Output: 5
Explanation: We can make 25 equal to 30 by applying the following operations:
1. Increment x by 1
2. Increment x by 1
3. Increment x by 1
4. Increment x by 1
5. Increment x by 1
It can be shown that 5 is the minimum number of operations required to make 25 equal to 30.
Problem summary: You are given two positive integers x and y. In one operation, you can do one of the four following operations: Divide x by 11 if x is a multiple of 11. Divide x by 5 if x is a multiple of 5. Decrement x by 1. Increment x by 1. Return the minimum number of operations required to make x and y equal.
Baseline thinking
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Dynamic Programming
Example 1
26
1
Example 2
54
2
Example 3
25
30
Related Problems
Shortest Bridge (shortest-bridge)
Minimum Moves to Spread Stones Over Grid (minimum-moves-to-spread-stones-over-grid)
Step 02
Core Insight
What unlocks the optimal approach
The only way to make <code>x</code> larger is to increase it by <code>1</code> so if <code>y >= x</code> the answer is <code>y - x</code>.
For <code>y < x</code>, <code>x - y</code> is always a candidate answer since we can repeatedly decrease <code>x</code> by one to reach <code>y</code>.
We can also increase <code>x</code> and then use the division operations. For example, if <code>x = 10</code> and <code>y = 1</code>, we can increment <code>x</code> by <code>1</code> then divide it by <code>11</code>.
Find an upper bound <code>U</code> on the maximum value of <code>x</code> we will reach an optimal solution. Since all values of <code>x</code> will be in the range <code>[1, U]</code>, we can use BFS to find the answer.
One possible upper bound on <code>x</code> is <code>U = x + (x - y) </code>. To reach any number strictly greater than <code>U</code> from <code>x</code>, we will need more than <code>x - y</code> operations which is not optimal since we can always reach <code>y</code> in <code>x - y</code> operations.
Interview move: turn each hint into an invariant you can check after every iteration/recursion step.
Step 03
Algorithm Walkthrough
Iteration Checklist
Define state (indices, window, stack, map, DP cell, or recursion frame).
Apply one transition step and update the invariant.
Record answer candidate when condition is met.
Continue until all input is consumed.
Use the first example testcase as your mental trace to verify each transition.
Step 04
Edge Cases
Minimum Input
Single element / shortest valid input
Validate boundary behavior before entering the main loop or recursion.
Duplicates & Repeats
Repeated values / repeated states
Decide whether duplicates should be merged, skipped, or counted explicitly.
Extreme Constraints
Upper-end input sizes
Re-check complexity target against constraints to avoid time-limit issues.
Invalid / Corner Shape
Empty collections, zeros, or disconnected structures
Handle special-case structure before the core algorithm path.
Step 05
Full Annotated Code
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2998: Minimum Number of Operations to Make X and Y Equal
class Solution {
private Map<Integer, Integer> f = new HashMap<>();
private int y;
public int minimumOperationsToMakeEqual(int x, int y) {
this.y = y;
return dfs(x);
}
private int dfs(int x) {
if (y >= x) {
return y - x;
}
if (f.containsKey(x)) {
return f.get(x);
}
int ans = x - y;
int a = x % 5 + 1 + dfs(x / 5);
int b = 5 - x % 5 + 1 + dfs(x / 5 + 1);
int c = x % 11 + 1 + dfs(x / 11);
int d = 11 - x % 11 + 1 + dfs(x / 11 + 1);
ans = Math.min(ans, Math.min(a, Math.min(b, Math.min(c, d))));
f.put(x, ans);
return ans;
}
}
// Accepted solution for LeetCode #2998: Minimum Number of Operations to Make X and Y Equal
func minimumOperationsToMakeEqual(x int, y int) int {
f := map[int]int{}
var dfs func(int) int
dfs = func(x int) int {
if y >= x {
return y - x
}
if v, ok := f[x]; ok {
return v
}
a := x%5 + 1 + dfs(x/5)
b := 5 - x%5 + 1 + dfs(x/5+1)
c := x%11 + 1 + dfs(x/11)
d := 11 - x%11 + 1 + dfs(x/11+1)
f[x] = min(x-y, a, b, c, d)
return f[x]
}
return dfs(x)
}
# Accepted solution for LeetCode #2998: Minimum Number of Operations to Make X and Y Equal
class Solution:
def minimumOperationsToMakeEqual(self, x: int, y: int) -> int:
@cache
def dfs(x: int) -> int:
if y >= x:
return y - x
ans = x - y
ans = min(ans, x % 5 + 1 + dfs(x // 5))
ans = min(ans, 5 - x % 5 + 1 + dfs(x // 5 + 1))
ans = min(ans, x % 11 + 1 + dfs(x // 11))
ans = min(ans, 11 - x % 11 + 1 + dfs(x // 11 + 1))
return ans
return dfs(x)
// Accepted solution for LeetCode #2998: Minimum Number of Operations to Make X and Y Equal
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2998: Minimum Number of Operations to Make X and Y Equal
// class Solution {
// private Map<Integer, Integer> f = new HashMap<>();
// private int y;
//
// public int minimumOperationsToMakeEqual(int x, int y) {
// this.y = y;
// return dfs(x);
// }
//
// private int dfs(int x) {
// if (y >= x) {
// return y - x;
// }
// if (f.containsKey(x)) {
// return f.get(x);
// }
// int ans = x - y;
// int a = x % 5 + 1 + dfs(x / 5);
// int b = 5 - x % 5 + 1 + dfs(x / 5 + 1);
// int c = x % 11 + 1 + dfs(x / 11);
// int d = 11 - x % 11 + 1 + dfs(x / 11 + 1);
// ans = Math.min(ans, Math.min(a, Math.min(b, Math.min(c, d))));
// f.put(x, ans);
// return ans;
// }
// }
// Accepted solution for LeetCode #2998: Minimum Number of Operations to Make X and Y Equal
function minimumOperationsToMakeEqual(x: number, y: number): number {
const f: Map<number, number> = new Map();
const dfs = (x: number): number => {
if (y >= x) {
return y - x;
}
if (f.has(x)) {
return f.get(x)!;
}
const a = (x % 5) + 1 + dfs((x / 5) | 0);
const b = 5 - (x % 5) + 1 + dfs(((x / 5) | 0) + 1);
const c = (x % 11) + 1 + dfs((x / 11) | 0);
const d = 11 - (x % 11) + 1 + dfs(((x / 11) | 0) + 1);
const ans = Math.min(x - y, a, b, c, d);
f.set(x, ans);
return ans;
};
return dfs(x);
}
Step 06
Interactive Study Demo
Use this to step through a reusable interview workflow for this problem.
Press Step or Run All to begin.
Step 07
Complexity Analysis
Time
O(n × m)
Space
O(n × m)
Approach Breakdown
RECURSIVE
O(2ⁿ) time
O(n) space
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
DYNAMIC PROGRAMMING
O(n × m) time
O(n × m) space
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Shortcut: Count your DP state dimensions → that’s your time. Can you drop one? That’s your space optimization.
Coach Notes
Common Mistakes
Review these before coding to avoid predictable interview regressions.
State misses one required dimension
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.