-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Partitions with given difference
- Loading branch information
1 parent
387ca53
commit de01d89
Showing
1 changed file
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
//Partitions with given difference | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
class IntArray { | ||
public static int[] input(BufferedReader br, int n) throws IOException { | ||
String[] s = br.readLine().trim().split(" "); | ||
int[] a = new int[n]; | ||
|
||
for(int i = 0; i < n; i++) | ||
a[i] = Integer.parseInt(s[i]); | ||
|
||
return a; | ||
} | ||
|
||
public static void print(int[] a) { | ||
for(int e : a) | ||
System.out.print(e + " "); | ||
System.out.println(); | ||
} | ||
|
||
public static void print(ArrayList<Integer> a) { | ||
for(int e : a) | ||
System.out.print(e + " "); | ||
System.out.println(); | ||
} | ||
} | ||
|
||
class GFG { | ||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int t = Integer.parseInt(br.readLine()); | ||
|
||
while(t-- > 0) { | ||
int n = Integer.parseInt(br.readLine()); | ||
int d = Integer.parseInt(br.readLine()); | ||
int[] arr = IntArray.input(br, n); | ||
Solution obj = new Solution(); | ||
int res = obj.countPartitions(n, d, arr); | ||
System.out.println(res); | ||
} | ||
} | ||
} | ||
|
||
class Solution { | ||
static int mod = 1000000007; | ||
|
||
static int help(int n, int d, int[] arr, int sum, int temp, int index, int dp[][]) { | ||
if(sum - temp - temp < d) { | ||
return 0; | ||
} | ||
|
||
if(index == n) { | ||
int secondSum = sum - temp; | ||
|
||
if(secondSum - temp == d) { | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
if(dp[index][temp] != -1) { | ||
return dp[index][temp]; | ||
} | ||
|
||
int a = help(n, d, arr, sum, temp, index + 1, dp); | ||
int b = help(n, d, arr, sum, temp + arr[index], index + 1, dp); | ||
|
||
return dp[index][temp] = (a + b) % mod; | ||
} | ||
|
||
public static int countPartitions(int n, int d, int[] arr) { | ||
int sum = 0; | ||
|
||
for(int i = 0; i < n; i++) { | ||
sum += arr[i]; | ||
} | ||
|
||
int dp[][] = new int[n][sum + 1]; | ||
|
||
for(int x[] : dp) { | ||
Arrays.fill(x, -1); | ||
} | ||
|
||
return help(n, d, arr, sum, 0, 0, dp); | ||
} | ||
} |