-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountSubsequences.java
More file actions
32 lines (32 loc) · 925 Bytes
/
Copy pathCountSubsequences.java
File metadata and controls
32 lines (32 loc) · 925 Bytes
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
import java.util.*;
public class CountSubsequences {
static int printSub(int ind, int arr[], int s, int sum){
if(ind==arr.length){
if(s==sum){
return 1;
}
else
return 0;
}
else{
s=s+arr[ind];
int l=printSub(ind+1, arr,s,sum);
s=s-arr[ind];
int r=printSub(ind+1,arr,s,sum);
return l+r;
}
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the array size");
int n=sc.nextInt();
int [] arr=new int[n];
System.out.println("Enter the array elements");
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
System.out.println("Enter the sum");
int sum=sc.nextInt();
System.out.println("The number of subsequnces formed for the given sum of "+ sum+" is "+printSub(0,arr,0,sum));
}
}