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. (Inclusive of L and R).
For example, if the given T is 2, read the L and R of the first test case in the next line. If the given L and R are 1 and 10 respectively. As 9 is the maximum product of digits, so the output for the first test case is 9.
If the L and R are 15 to 30 respectively. The product of the digits of number 29 is 18. As 18 is the maximum product in the range, so the output should be 18.
input:
2 1
1 10
15 30
output:
9
29
n = int(input())
numbers = []
for i in range(n):
numbers.append([int(n) for n in input().split()])
productDigits = []
for i in range(n):
maxValue = 0
maxNumber = 0
for number in range(numbers[i][0],numbers[i][1] + 1):
product = 1
tempNumber = number
while True:
product *= (tempNumber%10)
tempNumber = tempNumber // 10
if tempNumber == 0:
if product > maxValue:
maxValue = product
maxNumber = number
break
productDigits.append(maxNumber)
for pd in productDigits:
print(pd)
Comments
Leave a comment