Class Solution
-
- All Implemented Interfaces:
public final class Solution3377 - Digit Operations to Make Two Integers Equal.
Medium
You are given two integers
nandmthat consist of the same number of digits.You can perform the following operations any number of times:
Choose any digit from
nthat is not 9 and increase it by 1.Choose any digit from
nthat is not 0 and decrease it by 1.
The integer
nmust not be a prime number at any point, including its original value and after each operation.The cost of a transformation is the sum of all values that
ntakes throughout the operations performed.Return the minimum cost to transform
nintom. If it is impossible, return -1.A prime number is a natural number greater than 1 with only two factors, 1 and itself.
Example 1:
Input: n = 10, m = 12
Output: 85
Explanation:
We perform the following operations:
Increase the first digit, now <code>n = <ins> 2 </ins>0</code>.
Increase the second digit, now <code>n = 2**<ins>1</ins>**</code>.
Increase the second digit, now <code>n = 2**<ins>2</ins>**</code>.
Decrease the first digit, now <code>n = **<ins>1</ins>**2</code>.
Example 2:
Input: n = 4, m = 8
Output: \-1
Explanation:
It is impossible to make
nequal tom.Example 3:
Input: n = 6, m = 2
Output: \-1
Explanation:
Since 2 is already a prime, we can't make
nequal tom.Constraints:
<code>1 <= n, m < 10<sup>4</sup></code>
nandmconsist of the same number of digits.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegerminOperations(Integer n, Integer m)-
-
Method Detail
-
minOperations
final Integer minOperations(Integer n, Integer m)
-
-
-
-