Easy
You are given a positive integer n
.
Return the maximum product of any two digits in n
.
Note: You may use the same digit twice if it appears more than once in n
.
Example 1:
Input: n = 31
Output: 3
Explanation:
n
are [3, 1]
.3 * 1 = 3
.Example 2:
Input: n = 22
Output: 4
Explanation:
n
are [2, 2]
.2 * 2 = 4
.Example 3:
Input: n = 124
Output: 8
Explanation:
n
are [1, 2, 4]
.1 * 2 = 2
, 1 * 4 = 4
, 2 * 4 = 8
.Constraints:
10 <= n <= 109
class Solution {
fun maxProduct(n: Int): Int {
var n = n
var m1 = n % 10
n /= 10
var m2 = n % 10
n /= 10
while (n > 0) {
val a = n % 10
if (a > m1) {
if (m1 > m2) {
m2 = m1
}
m1 = a
} else {
if (a > m2) {
m2 = a
}
}
n /= 10
}
return m1 * m2
}
}