Skip to content

Practice Digit Operations to Make Two Integers Equal

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3377
LeetCode Problem

Digit Operations to Make Two Integers Equal

Problem Statement

Given equal-digit-length integers n and m, one operation increases a digit other than 9 by 1 or decreases a digit other than 0 by 1. The value of n must never be prime, including initially and after every operation. The cost is the sum of all visited integer values, including the starting n and final m. Return the minimum cost to reach m, or -1 if impossible.

Source: Digit Operations to Make Two Integers Equal on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 10, m = 12
Output
85

# Example 2

Input
n = 4, m = 8
Output
-1

# Example 3

Input
n = 6, m = 2
Output
-1

Constraints

1 <= n, m < 10^4
n and m consist of the same number of digits.

Related LeetCode Problems

Practice more problems similar to Digit Operations to Make Two Integers Equal.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.