Advertisement
unknown_0711

Untitled

Dec 17th, 2022
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7. import java.util.*;
  8.  
  9. class Solution {
  10.  
  11. public static void solve(int []coins, int n, int sum, int target, String temp, int []vis){
  12. if(sum > target)
  13. return;
  14. if(sum == target){
  15. System.out.println(temp);
  16. return;
  17. }
  18.  
  19. for(int i=0; i<n; i++){
  20. if(vis[i] == 0){
  21. vis[i] = 1;
  22. solve(coins, n, sum+coins[i], target, temp+coins[i]+" ", vis);
  23. vis[i] = 0;
  24. }
  25. }
  26. }
  27. public static void coinChange(int[] coins, int target){
  28.  
  29. int n=coins.length;
  30. int []vis = new int[n];
  31. solve(coins, n, 0, target, "", vis);
  32. }
  33.  
  34. }
  35.  
  36. public class Main {
  37. public static void main(String[] args) {
  38. Scanner sc = new Scanner(System.in);
  39. int n = sc.nextInt();
  40. int[] coins = new int[n];
  41. for (int i = 0; i < n; i++) {
  42. coins[i] = sc.nextInt();
  43. }
  44. int amt = sc.nextInt();
  45.  
  46. Solution Obj = new Solution();
  47. Obj.coinChange(coins, amt);
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement