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)
5 odpowiedzi
+ 1
This should do it:
https://code.sololearn.com/cuM59DfNvLSx/?ref=app
+ 1
thanks a lot
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
}
0
output should be (1,4) (2,3) (5)
(first==sum) gives output: 5
:(
0
Both if statements goes into your loop.