Untitled
user_0781376
plain_text
13 days ago
857 B
14
Indexable
import java.util.*; public class Main { public static void findMinCoins(int[] coins, int amount) { Arrays.sort(coins); // Sort coins in ascending order List<Integer> result = new ArrayList<>(); // Start from the largest coin and work down for (int i = coins.length - 1; i >= 0; i--) { while (amount >= coins[i]) { amount -= coins[i]; result.add(coins[i]); // Store the used coin } } // Output the result System.out.println("Minimum coins needed: " + result.size()); System.out.println("Coins used: " + result); } public static void main(String[] args) { int[] coins = {1, 2, 5, 10, 20, 50, 100, 500, 2000}; int amount = 2890; findMinCoins(coins, amount); } }
Editor is loading...
Leave a Comment