Solution: Find the Largest Number Possible
In this review, we will give a detailed analysis of the solution to the previous challenge.
We'll cover the following...
Solution: greedy approach
def find_largest_number(number_of_digits, sum_of_digits):"""Finds the largest number with given number of digits and sum of Digits:param number_of_digits: Number of digits:param sum_of_digits: Sum of digits:return: Possible largest number"""# If the sum of digits is 0, then a number is possible only if the number of digits is 1.if sum_of_digits == 0:if number_of_digits == 1:return [0]else:return [-1]# sum_of_digits is greater than the maximum possible sum.if sum_of_digits > 9 * number_of_digits:return [-1]result = [0] * number_of_digits# Fill from most significant digit to least significant digit!for i in range(number_of_digits):# Place 9 to make the number largestif sum_of_digits >= 9:result[i] = 9sum_of_digits -= 9# If remaining sum becomes less than 9, then fill the remaining sumelse:result[i] = sum_of_digitssum_of_digits = 0return result# Driver code to test above functionif __name__ == '__main__':sum_of_digits = 20number_of_digits = 3print(find_largest_number(number_of_digits, sum_of_digits))sum_of_digits = 100number_of_digits = 2print(find_largest_number(number_of_digits, sum_of_digits))
Explanation
We can solve the ...
Access this course and 1400+ top-rated courses and projects.