codextj

target_sum_set_soln_1_java

Sep 10th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3. import java.util.ArrayList;
  4.  
  5. public class SumSet {
  6.     static void sum_up_recursive(int start, ArrayList<Integer> partial) {
  7.        
  8.        //System.out.println(Arrays.toString(partial.toArray())); //print this to understand
  9.        int sum = 0;
  10.        
  11.        for (int x: partial) sum += x;
  12.        
  13.        //base conditions this will stop recursion
  14.         if (sum == target && partial.size() == k){
  15.             ansCnt += 1;
  16.             System.out.println("ans "+ansCnt+": "+Arrays.toString(partial.toArray()));
  17.             return;
  18.         }
  19.            
  20.         if (sum > target || partial.size() > k){
  21.             return;
  22.         }
  23.            
  24.         for(int i=start; i<numbers.length; i++) {
  25.             start = i+1;
  26.             ArrayList<Integer> partial_rec = new ArrayList<Integer>(partial);
  27.             partial_rec.add(numbers[i]);
  28.             sum_up_recursive(start, partial_rec);
  29.        }
  30.     }
  31.    
  32.    
  33.     static int[] numbers;
  34.     static int target;
  35.     static int k;
  36.     static int start =0;
  37.     static int ansCnt = 0;
  38.    
  39.    
  40.     public static void main(String args[]) {
  41.         Scanner scan = new Scanner(System.in);
  42.        
  43.         int n = scan.nextInt();
  44.         target = scan.nextInt();
  45.         k = scan.nextInt();
  46.        
  47.         numbers = new int[n];
  48.         for(int i =0; i < n; i++){
  49.             numbers[i] = scan.nextInt();
  50.         }
  51.        
  52.         Arrays.sort(numbers); // required
  53.        
  54.         // optional
  55.         int idx = Arrays.binarySearch(numbers,0,numbers.length,target);
  56.        
  57.        
  58.         if(k == 1){
  59.             if(idx > -1){
  60.                 System.out.print("ans 1: "+numbers[idx]);
  61.             }
  62.             else{
  63.                 System.out.print("no solution");
  64.             }
  65.         }
  66.         else{
  67.             sum_up_recursive(start, new ArrayList<Integer>());
  68.             if(ansCnt == 0) System.out.println("no solution");
  69.         }
  70.        
  71.        
  72.     }
  73. }
  74.  
  75. // reference https://stackoverflow.com/questions/4632322/finding-all-possible-combinations-of-numbers-to-reach-a-given-sum
  76.  
  77. /*
  78. input:
  79. 6 <--n
  80. 10 <-- target
  81. 4 <--k
  82. 3 7 1 9 -1 5
  83. -1 -2 9 3 10 5
  84.  
  85. output:
  86. ans 1: [-1, 1, 3, 7]
  87.  
  88.  
  89. */
  90.  
  91. /*
  92.  
  93. don't slice array based on target
  94. edge case:
  95. 6
  96. 6
  97. 3
  98. -1 -2 9 3 10 5 // due to negative number even number 9 can be used to generate set [-2, -1, 9]
  99. ans 1: [-2, -1, 9]
  100. ans 2: [-2, 3, 5]
  101. */
Add Comment
Please, Sign In to add comment