Back to Month
EASY 25 Jul 2026 View on LeetCode

3536. Maximum Product of Two Digits

</> Solution

class Solution {
    public int maxProduct(int n) {
        int first = 0;
        int second = 0;
        while (n > 0) {
            int digit = n % 10;
            if (digit >= first) {
                second = first;
                first = digit;
            } else if (digit > second) {
                second = digit;
            }
            n /= 10;
        }
        return first * second;
    }
}

TIME COMPLEXITY

O(log n)

SPACE COMPLEXITY

O(1)

TOPICS

Greedy Math