Correct Change
Collapse Content
Show Content
In the Kingdom of Zumbania, the Currency printer can only print the digit 1
. Thus they have 3 different denominations: 1
, 11
, and 111
. You need to determine if you can reach a certain sum with a given set of coins.
You will be given a line of input (as an array) consisting of 4 integers for the number of 1-coins, 11-coins, 111-coins, and the desired sum.
For example, the input 1 2 3 356
means there's 1
1-coin, 2
11-coins, 3
111-coins and a desired sum of 356
.
Print true
if you can select the correct sum from the given coins, and false
otherwise.
Sample Cases
- Given
1 2 3 356
as input, print outtrue
, since the sum of all the coins is356
(1*1 + 11*2 + 3*111 = 356). - Given
2 2 2 269
, print outfalse
, since there's no way to reach the given sum. - Given
0 11 5 565
printtrue
, since you can take all 11 11-coins and 4 111-coins for a total sum of565
.
Input Details
- The input starts with N, the number of cases.
- N cases follow, which each contain the number 4, followed by 4 numbers...
- ...containing the number of pennies, eleven-coins, 111-coins, and the desired sum
Challenge
Print true if you can get the exact sum and false otherwise.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Florian K
May 26, 9:37 AMshouldn't the 3rd Test Input be true too? 3 (3*1) + 33 (3*11) + 333 (3*111) = 369
Learneroo
May 26, 9:39 AM@Florian, you need to select the correct change exactly. 369 is not equal to 365, and there's no way to get 365 with those coins.
Patrick Lu
Jul 1, 6:23 PMI have a solution but I think there is a better way to do it. Is there a solution that we can see?
Learneroo
Jul 1, 7:27 PM@Patrick Lu, I added a featured answer, see also the Learneroo blog.