Challenge: Find the Largest Number Possible
Find the largest number that can be created from a set of digits and their sum.
We'll cover the following
Problem statement
Suppose a girl lost the key to her locker (the key is only numeric). She remembers the number of digits as well as the sum of all the digits of her password. She also knows that her password consists of the largest possible digits that add up to the sum of .
Implement a function that would help her retrieve her key.
Input
The number of digits and the sum of those digits.
Output
The largest number that can be created (or the key) as an array.
Sample input
sumOfDigits = 20;
numberOfDigits = 3;
Sample output
result = {9, 9, 2};
Note: If no number is possible for a combination of
sumOfDigits
andnumberOfDigits
, simply return[-1]
.
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.