#3377

Digit Operations to Make Two Integers Equal

expert · 1135 · lc medium +32 · 29.9% accepted · 132 likes · top 7%

Description

You are given two integers n and m with the same number of digits.

You may perform any number of operations on n:

- Pick any digit of n that is not 9 and increase it by 1.

- Pick any digit of n that is not 0 and decrease it by 1.

At no point (including the initial value and after each operation) may n be a prime number.

The cost of the entire transformation is the sum of every value n takes throughout the operations.

Return the minimum cost to transform n into m, or -1 if it is impossible.

Code

1
2
3