Choose two different decimal digit positions in a positive integer and return their maximum product. Equal digit values may be used together only when they occur at least twice.
# Maximum Product of Two Decimal Digits
Given a positive integer `n`, choose two different digit positions in its decimal representation and return the largest possible product of the two selected digits.
Equal digit values may be selected together only when they occur at least twice. The positions need not be adjacent.
### C++ Interface
```cpp
class Solution {
public:
int maxProduct(int n);
};
```
### Examples
**Example 1**
```text
n = 9092
Output: 81
```
The two occurrences of digit nine can both be selected, giving `9 * 9 = 81`.
**Example 2**
```text
n = 5073
Output: 35
```
Selecting five and seven gives `35`. The other pair products are `0`, `15`, `0`, `0`, and `21`, so none is larger.
### Constraints
- `10 <= n <= 1000000000`.
- Digits are taken from the ordinary decimal representation of `n`.
### Clarifying Questions
- Can one digit occurrence be reused? No. Using the same digit value twice requires two occurrences in `n`.
- Must the selected digits be neighbors? No. Any two different positions are allowed.
- Are zero digits eligible? Yes. Selecting a zero makes that pair's product zero.
- Could there be fewer than two digit positions? No; the lower bound on `n` guarantees at least two.
### What a Strong Answer Covers
- Selection of exactly two existing digit occurrences.
- Correct treatment of repeated digit values and zeros.
- The maximum over all allowed position pairs, regardless of adjacency.
- Time and space bounds for at most ten decimal digit positions.
### Follow-up Questions
- Can a positive input in the stated range have maximum product zero? Give an example.
- Would rearranging the same digit occurrences change which pair products are possible?
```hint Consider each pair
Extract the decimal digits, then examine every pair of different positions. The input has at most ten digits, so comparing all such products is small.
```
Overview: Choose two different decimal digit positions in a positive integer and return their maximum product. Equal digit values may be used together only when they occur at least twice.
Given a positive integer n, choose two different digit positions in its decimal representation and return the largest possible product of the two selected digits.
Equal digit values may be selected together only when they occur at least twice. The positions need not be adjacent.
C++ Interface
class Solution {
public:
int maxProduct(int n);
};
Examples
Example 1
n = 9092
Output: 81
The two occurrences of digit nine can both be selected, giving 9 * 9 = 81.
Example 2
n = 5073
Output: 35
Selecting five and seven gives 35. The other pair products are 0, 15, 0, 0, and 21, so none is larger.
Constraints
10 <= n <= 1000000000
.
Digits are taken from the ordinary decimal representation of
n
.
Clarifying Questions Guidance
Can one digit occurrence be reused? No. Using the same digit value twice requires two occurrences in
n
.
Must the selected digits be neighbors? No. Any two different positions are allowed.
Are zero digits eligible? Yes. Selecting a zero makes that pair's product zero.
Could there be fewer than two digit positions? No; the lower bound on
n
guarantees at least two.
What a Strong Answer Covers Guidance
Selection of exactly two existing digit occurrences.
Correct treatment of repeated digit values and zeros.
The maximum over all allowed position pairs, regardless of adjacency.
Time and space bounds for at most ten decimal digit positions.
Follow-up Questions Guidance
Can a positive input in the stated range have maximum product zero? Give an example.
Would rearranging the same digit occurrences change which pair products are possible?