0

find out count of all subsets such that the sum of all the numbers in the subset is equal to target number.

input :[1,2,3,4,5]. target :5. count: [1,4] [2,3] [5] for(int i=0; i<array.length ; i++) { int first=array[i] ; for(int j=i+1; j<array.length ; j++) { int second=array[j] ; if((first+second)==sum) { system.out.printf("%d, %d) %n, first, second); }}} this code gives me output (1,4) (2,3). output should be (1,4) (2,3) (5) please help me with (5)

18th Sep 2019, 8:51 AM
vivian
5 Respuestas
18th Sep 2019, 2:42 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
thanks a lot
18th Sep 2019, 2:46 PM
vivian
0
The code looks good. You just need to check if first[i] == sum. //when the number itself is the target if(first == sum){ //print first } //when the sum of two numbers is the target if(first + second == sum){ //print first and second }
18th Sep 2019, 2:12 PM
Denise Roßberg
Denise Roßberg - avatar
0
output should be (1,4) (2,3) (5) (first==sum) gives output: 5 :(
18th Sep 2019, 2:19 PM
vivian
0
Both if statements goes into your loop.
18th Sep 2019, 2:24 PM
Denise Roßberg
Denise Roßberg - avatar