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.
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Find Next number having distinct digits from the given number N
- Print all numbers in given range having digits in strictly increasing order
- Minimum digits to be removed to make either all digits or alternating digits same
- Range Queries to find the Element having Maximum Digit Sum
- Find maximum product of digits among numbers less than or equal to N
- Find the Largest number with given number of digits and sum of digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Queries for elements having values within the range A to B in the given index range using Segment Tree
- Maximum of sum and product of digits until number is reduced to a single digit
- Maximum sum and product of the M consecutive digits in a number
- Count numbers in range such that digits in it and it's product with q are unequal
- Cumulative product of digits of all numbers in the given range
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Queries to find maximum product pair in range with updates
- Program to find count of numbers having odd number of divisors in given range
- Find the average of k digits from the beginning and l digits from the end of the given number
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.