forked from izzarzn/cselab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.java
49 lines (46 loc) · 1.11 KB
/
11.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.Scanner;
public class Sumofsubset {
static int c=0;
public static void main(String[] args) {
int w[]=new int[10];
int n,d,i,sum=0;
int x[]=new int[10];
Scanner read=new Scanner(System.in);
System.out.println("enter the number of elements:");
n=read.nextInt();
System.out.println("enter the elements in increasing order:");
for(i=0;i<=n;i++)
w[i]=read.nextInt();
System.out.println("enter the value of d:");
d=read.nextInt();
for(i=0;i<n;i++)
sum=sum+w[i];
if(sum<d||w[0]>d)
{
System.out.println("subset not possible!");
System.exit(0);
}
finalsubset(0,0,sum,x,w,d);
if(c==0)
System.out.println("subset is not possible!");
}
static void finalsubset(int a,int k,int r,int x[],int w[],int d)
{
x[k]=1;
if(a+w[k]==d)
{c++;
System.out.print("\n solution "+c+" is {");
for( int i=0;i<=k;i++)
if(x[i]==1)
{
System.out.print(w[i]+" ");
}
System.out.println("}");
}
else if (a+w[k]+w[k+1]<=d)
finalsubset(a+w[k],k+1,r-w[k],x,w,d);
if((a + r - w[k] ) >= d && ( a + w[k] ) <= d)
{
x[k]=0;
finalsubset(a,k+1,r-w[k],x,w,d);
} } }