Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,23 @@ public static void main(String[] args) {
}

private static List<Integer> getChange(int sum, int[] coins) {
if (coins == null || coins.length == 0){
throw new IndexOutOfBoundsException("Invalid coins array.");
}

List<Integer> result = new ArrayList<>();
// You code here
return result;
List<Integer> coinsList = Arrays.stream(coins).boxed().sorted(Comparator.reverseOrder()).toList();
int sumCopy = sum;
for (int coin : coinsList) {
while (sumCopy >= coin) {
if (sumCopy == coin) {
result.add(coin);
return result;
}
sumCopy -= coin;
result.add(coin);
}
}
return new ArrayList<>();
}
}