# Find the number in a range having maximum product of the digits

Given a range represented by two positive integers L and R. Find the number lying in the range having the maximum product of the digits.

**Examples:**

Input :L = 1, R = 10Output :9Input :L = 51, R = 62Output :59

**Approach :** The key idea here is to iterate over the digits of the number R starting from the most significant digit. Going from left to right, i.e. from most sigificant digit to the least significant digit, replace the current digit with one less than current digit and replace all the digits after current digit in the number with 9, since the number has already become smaller than R at the current position so we can safely put any number in the following digits to maximize the product of digits. Also, check if the resulting number is greater than L to remain in the range and update the maximum product.

Below is the implementation of the above approach:

## C++

`// CPP Program to find the number in a ` `// range having maximum product of the ` `// digits ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns the product of digits of number x ` `int` `product(` `int` `x) ` `{ ` ` ` `int` `prod = 1; ` ` ` `while` `(x) { ` ` ` `prod *= (x % 10); ` ` ` `x /= 10; ` ` ` `} ` ` ` `return` `prod; ` `} ` ` ` `// This function returns the number having ` `// maximum product of the digits ` `int` `findNumber(` `int` `l, ` `int` `r) ` `{ ` ` ` `// Converting both integers to strings ` ` ` `string a = to_string(l); ` ` ` `string b = to_string(r); ` ` ` ` ` `// Let the current answer be r ` ` ` `int` `ans = r; ` ` ` `for` `(` `int` `i = 0; i < b.size(); i++) { ` ` ` `if` `(b[i] == ` `'0'` `) ` ` ` `continue` `; ` ` ` ` ` `// Stores the current number having ` ` ` `// current digit one less than current ` ` ` `// digit in b ` ` ` `string curr = b; ` ` ` `curr[i] = ((curr[i] - ` `'0'` `) - 1) + ` `'0'` `; ` ` ` ` ` `// Replace all following digits with 9 ` ` ` `// to maximise the product ` ` ` `for` `(` `int` `j = i + 1; j < curr.size(); j++) ` ` ` `curr[j] = ` `'9'` `; ` ` ` ` ` `// Convert string to number ` ` ` `int` `num = 0; ` ` ` `for` `(` `auto` `c : curr) ` ` ` `num = num * 10 + (c - ` `'0'` `); ` ` ` ` ` `// Check if it lies in range and its product ` ` ` `// is greater than max product ` ` ` `if` `(num >= l && product(ans) < product(num)) ` ` ` `ans = num; ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `l = 1, r = 10; ` ` ` `cout << findNumber(l, r) << endl; ` ` ` ` ` `l = 51, r = 62; ` ` ` `cout << findNumber(l, r) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 Program to find the number ` `# in a range having maximum product ` `# of the digits ` ` ` `# Returns the product of digits ` `# of number x ` `def` `product(x) : ` ` ` ` ` `prod ` `=` `1` ` ` `while` `(x) : ` ` ` `prod ` `*` `=` `(x ` `%` `10` `) ` ` ` `x ` `/` `/` `=` `10` `; ` ` ` ` ` `return` `prod ` ` ` `# This function returns the number having ` `# maximum product of the digits ` `def` `findNumber(l, r) : ` ` ` ` ` `# Converting both integers to strings ` ` ` `a ` `=` `str` `(l); ` ` ` `b ` `=` `str` `(r); ` ` ` ` ` `# Let the current answer be r ` ` ` `ans ` `=` `r ` ` ` ` ` `for` `i ` `in` `range` `(` `len` `(b)) : ` ` ` `if` `(b[i] ` `=` `=` `'0'` `) : ` ` ` `continue` ` ` ` ` `# Stores the current number having ` ` ` `# current digit one less than current ` ` ` `# digit in b ` ` ` `curr ` `=` `list` `(b) ` ` ` `curr[i] ` `=` `str` `(((` `ord` `(curr[i]) ` `-` ` ` `ord` `(` `'0'` `)) ` `-` `1` `) ` `+` `ord` `(` `'0'` `)) ` ` ` ` ` `# Replace all following digits with 9 ` ` ` `# to maximise the product ` ` ` `for` `j ` `in` `range` `(i ` `+` `1` `, ` `len` `(curr)) : ` ` ` `curr[j] ` `=` `str` `(` `ord` `(` `'9'` `)) ` ` ` ` ` `# Convert string to number ` ` ` `num ` `=` `0` ` ` `for` `c ` `in` `curr : ` ` ` `num ` `=` `num ` `*` `10` `+` `(` `int` `(c) ` `-` `ord` `(` `'0'` `)) ` ` ` ` ` `# Check if it lies in range and its ` ` ` `# product is greater than max product ` ` ` `if` `(num >` `=` `l ` `and` `product(ans) < product(num)) : ` ` ` `ans ` `=` `num ` ` ` ` ` `return` `ans ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `l, r ` `=` `1` `, ` `10` ` ` `print` `(findNumber(l, r)) ` ` ` ` ` `l, r ` `=` `51` `, ` `62` ` ` `print` `(findNumber(l, r)) ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

**Output:**

9 59

**Time Complexity**: O(18 * 18), if we are dealing with the numbers upto 10^{18}.

## Recommended Posts:

- Maximum sum and product of the M consecutive digits in a number
- Queries to find maximum product pair in range with updates
- Maximum of sum and product of digits until number is reduced to a single digit
- Find the Number of Maximum Product Quadruples
- Count numbers in range such that digits in it and it's product with q are unequal
- Find maximum number that can be formed using digits of a given number
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Number of digits in the product of two numbers
- Smallest number k such that the product of digits of k is equal to n
- Check whether product of digits at even places of a number is divisible by K
- Sum and Product of digits in a number that divide the number
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Find four factors of N with maximum product and sum equal to N | Set 3
- Find four factors of N with maximum product and sum equal to N

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.