Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.ByteArrayInputStream;
- import java.util.Scanner;
- public class SubsetOfSumS {
- static boolean subsetSum(int[] set, int index, int sum) {
- if (sum == 0) {
- return true;
- }
- if (sum < 0 || index >= set.length) {
- return false;
- }
- return subsetSum(set, index + 1, sum - set[index]) ||
- subsetSum(set, index + 1, sum);
- }
- static void testInput() {
- String test = "14\n" +
- "2 1 2 4 3 5 2 6";
- System.setIn(new ByteArrayInputStream(test.getBytes()));
- }
- public static void main(String[] args) {
- testInput();
- Scanner scanner = new Scanner(System.in);
- int sum = scanner.nextInt();
- scanner.nextLine();
- String[] setAsString = scanner.nextLine().split(" +");
- int[] set = new int[setAsString.length];
- for (int i = 0; i < set.length; i++) {
- set[i] = Integer.parseInt(setAsString[i]);
- }
- if(subsetSum(set, 0, sum) == true)
- {
- System.out.println("yes");
- }
- else
- {
- System.out.println("no");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement