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.
Input : L = 1, R = 10 Output : 9 Input : L = 51, R = 62 Output : 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:
Time Complexity: O(18 * 18), if we are dealing with the numbers upto 1018.
- Find maximum product of digits among numbers less than or equal to N
- 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
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Number of digits in the product of two numbers
- Smallest number k such that the product of digits of k is equal to n
- Sum and Product of digits in a number that divide the number
- Check if product of digits of a number at even and odd places is equal
- Check whether product of digits at even places of a number is divisible by K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.