|
| 1 | +//{ Driver Code Starts |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | + |
| 6 | +class IntArray |
| 7 | +{ |
| 8 | + public static int[] input(BufferedReader br, int n) throws IOException |
| 9 | + { |
| 10 | + String[] s = br.readLine().trim().split(" "); |
| 11 | + int[] a = new int[n]; |
| 12 | + for(int i = 0; i < n; i++) |
| 13 | + a[i] = Integer.parseInt(s[i]); |
| 14 | + |
| 15 | + return a; |
| 16 | + } |
| 17 | + |
| 18 | + public static void print(int[] a) |
| 19 | + { |
| 20 | + for(int e : a) |
| 21 | + System.out.print(e + " "); |
| 22 | + System.out.println(); |
| 23 | + } |
| 24 | + |
| 25 | + public static void print(ArrayList<Integer> a) |
| 26 | + { |
| 27 | + for(int e : a) |
| 28 | + System.out.print(e + " "); |
| 29 | + System.out.println(); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +class GFG { |
| 34 | + public static void main(String[] args) throws IOException { |
| 35 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 36 | + int t; |
| 37 | + t = Integer.parseInt(br.readLine()); |
| 38 | + while(t-- > 0){ |
| 39 | + |
| 40 | + int n; |
| 41 | + n = Integer.parseInt(br.readLine()); |
| 42 | + |
| 43 | + |
| 44 | + int d; |
| 45 | + d = Integer.parseInt(br.readLine()); |
| 46 | + |
| 47 | + |
| 48 | + int[] arr = IntArray.input(br, n); |
| 49 | + |
| 50 | + Solution obj = new Solution(); |
| 51 | + int res = obj.countPartitions(n, d, arr); |
| 52 | + |
| 53 | + System.out.println(res); |
| 54 | + |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// } Driver Code Ends |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +class Solution { |
| 64 | + static int mod=1000000007; |
| 65 | + static int help(int n,int d,int[] arr,int sum,int temp,int index,int dp[][]){ |
| 66 | + if(sum-temp-temp<d)return 0; |
| 67 | + if(index==n){ |
| 68 | + int secondSum=sum-temp; |
| 69 | + if(secondSum-temp==d)return 1; |
| 70 | + return 0; |
| 71 | + } |
| 72 | + if(dp[index][temp]!=-1)return dp[index][temp]; |
| 73 | + int a = help(n,d,arr,sum,temp,index+1,dp); |
| 74 | + int b = help(n,d,arr,sum,temp+arr[index],index+1,dp); |
| 75 | + return dp[index][temp] = (a+b)%mod; |
| 76 | + } |
| 77 | + public static int countPartitions(int n, int d, int[] arr) { |
| 78 | + // code here |
| 79 | + int sum=0; |
| 80 | + for(int i=0;i<n;i++)sum+=arr[i]; |
| 81 | + int dp[][] = new int[n][sum+1]; |
| 82 | + for(int x[]:dp)Arrays.fill(x,-1); |
| 83 | + return help(n,d,arr,sum,0,0,dp); |
| 84 | + } |
| 85 | +} |
| 86 | + |
0 commit comments