Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Solution {
- public static void solve(int []coins, int n, int sum, int target, String temp, int []vis){
- if(sum > target)
- return;
- if(sum == target){
- System.out.println(temp);
- return;
- }
- for(int i=0; i<n; i++){
- if(vis[i] == 0){
- vis[i] = 1;
- solve(coins, n, sum+coins[i], target, temp+coins[i]+" ", vis);
- vis[i] = 0;
- }
- }
- }
- public static void coinChange(int[] coins, int target){
- int n=coins.length;
- int []vis = new int[n];
- solve(coins, n, 0, target, "", vis);
- }
- }
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int n = sc.nextInt();
- int[] coins = new int[n];
- for (int i = 0; i < n; i++) {
- coins[i] = sc.nextInt();
- }
- int amt = sc.nextInt();
- Solution Obj = new Solution();
- Obj.coinChange(coins, amt);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement