TOYDESIRE Editorial

Problem Explanation

You are given an array of coins C and an integer X. You have to tell the minimum number of coins to make an amount greater than or equal to X or tell if it is impossible to do so.

Approach

We can sort the array in decreasing order and then iterate over the Coins array and add the coin value till we get a value greater than or equal to X. If we get the value set a flag to true and break out of the loop. Based on the flag we can tell if we found the value or not. If the flag is true, we output the value. Otherwise, we output -1 to indicate that it is impossible.