# Count ways to split array into two subsets having difference between their sum equal to K

Given an array** A[]** of size **N** and an integer **diff**, the task is to count the number of ways to split the array into two subsets (*non-empty subset is possible*) such that the difference between their sums is equal to **diff**.

**Examples:**

Input:A[] = {1, 1, 2, 3}, diff = 1Output:3Explanation:All possible combinations are as follows:

- {1, 1, 2} and {3}
- {1, 3} and {1, 2}
- {1, 2} and {1, 3}
All partitions have difference between their sums equal to 1. Therefore, the count of ways is 3.

Input:A[] = {1, 6, 11, 5}, diff=1Output:2

**Naive Approach: **The simplest approach to solve the problem is based on the following observations:

Let the sum of elements in the partition subsets

S1andS2besum1andsum2respectively.

Let sum of the arrayA[]beX.

Given,sum1 – sum2 = diff– (1)

Also,sum1 + sum2 = X– (2)From equations (1) and (2),

sum1 = (X + diff)/2

Therefore, the task is reduced to finding the number of subsets with a given sum.

Therefore, the simplest approach is to solve this problem is by generating all the possible subsets and checking whether the subset has the required sum.

**Time Complexity: **O(2^{N})**Auxiliary Space: **O(N)

**Efficient Approach: **To optimize the above approach, the idea is to use Dynamic Programming. Initialize a **dp[][]** table of size **N*X**, where **dp[i][C]** stores the number of subsets of the sub-array **A[iâ€¦N-1]** such that their sum is equal to **C**. Thus, the recurrence is very trivial as there are only two choices i.e. either consider the **i ^{th}** element in the subset or donâ€™t. So the recurrence relation will be:

dp[i][C] = dp[i – 1][C – A[i]] + dp[i-1][C]

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to count the number of ways to divide` `// the array into two subsets and such that the` `// difference between their sums is equal to diff` `int` `countSubset(` `int` `arr[], ` `int` `n, ` `int` `diff)` `{` ` ` `// Store the sum of the set S1` ` ` `int` `sum = 0;` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `sum += arr[i];` ` ` `sum += diff;` ` ` `sum = sum / 2;` ` ` `// Initializing the matrix` ` ` `int` `t[n + 1][sum + 1];` ` ` `// Number of ways to get sum` ` ` `// using 0 elements is 0` ` ` `for` `(` `int` `j = 0; j <= sum; j++)` ` ` `t[0][j] = 0;` ` ` `// Number of ways to get sum 0` ` ` `// using i elements is 1` ` ` `for` `(` `int` `i = 0; i <= n; i++)` ` ` `t[i][0] = 1;` ` ` `// Traverse the 2D array` ` ` `for` `(` `int` `i = 1; i <= n; i++) {` ` ` `for` `(` `int` `j = 1; j <= sum; j++) {` ` ` `// If the value is greater` ` ` `// than the sum store the` ` ` `// value of previous state` ` ` `if` `(arr[i - 1] > j)` ` ` `t[i][j] = t[i - 1][j];` ` ` `else` `{` ` ` `t[i][j] = t[i - 1][j]` ` ` `+ t[i - 1][j - arr[i - 1]];` ` ` `}` ` ` `}` ` ` `}` ` ` `// Return the result` ` ` `return` `t[n][sum];` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given Input` ` ` `int` `diff = 1, n = 4;` ` ` `int` `arr[] = { 1, 1, 2, 3 };` ` ` `// Function Call` ` ` `cout << countSubset(arr, n, diff);` `}` |

## Java

`// Java program for the above approach` `import` `java.io.*;` `public` `class` `GFG` `{` ` ` `// Function to count the number of ways to divide` ` ` `// the array into two subsets and such that the` ` ` `// difference between their sums is equal to diff` ` ` `static` `int` `countSubset(` `int` `[]arr, ` `int` `n, ` `int` `diff)` ` ` `{` ` ` ` ` `// Store the sum of the set S1` ` ` `int` `sum = ` `0` `;` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++)` ` ` `sum += arr[i];` ` ` `sum += diff;` ` ` `sum = sum / ` `2` `;` ` ` ` ` `// Initializing the matrix` ` ` `int` `t[][] = ` `new` `int` `[n + ` `1` `][sum + ` `1` `];` ` ` ` ` `// Number of ways to get sum` ` ` `// using 0 elements is 0` ` ` `for` `(` `int` `j = ` `0` `; j <= sum; j++)` ` ` `t[` `0` `][j] = ` `0` `;` ` ` ` ` `// Number of ways to get sum 0` ` ` `// using i elements is 1` ` ` `for` `(` `int` `i = ` `0` `; i <= n; i++)` ` ` `t[i][` `0` `] = ` `1` `;` ` ` ` ` `// Traverse the 2D array` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) {` ` ` `for` `(` `int` `j = ` `1` `; j <= sum; j++) {` ` ` ` ` `// If the value is greater` ` ` `// than the sum store the` ` ` `// value of previous state` ` ` `if` `(arr[i - ` `1` `] > j)` ` ` `t[i][j] = t[i - ` `1` `][j];` ` ` ` ` `else` `{` ` ` `t[i][j] = t[i - ` `1` `][j]` ` ` `+ t[i - ` `1` `][j - arr[i - ` `1` `]];` ` ` `}` ` ` `}` ` ` `}` ` ` ` ` `// Return the result` ` ` `return` `t[n][sum];` ` ` `}` ` ` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` ` ` `// Given Input` ` ` `int` `diff = ` `1` `, n = ` `4` `;` ` ` `int` `arr[] = { ` `1` `, ` `1` `, ` `2` `, ` `3` `};` ` ` ` ` `// Function Call` ` ` `System.out.print(countSubset(arr, n, diff));` ` ` `}` `}` `// This code is contributed by AnkThon` |

## Python3

`# Python3 program for the above approach` `# Function to count the number of ways to divide` `# the array into two subsets and such that the` `# difference between their sums is equal to diff` `def` `countSubset(arr, n, diff):` ` ` ` ` `# Store the sum of the set S1` ` ` `sum` `=` `0` ` ` `for` `i ` `in` `range` `(n):` ` ` `sum` `+` `=` `arr[i]` ` ` ` ` `sum` `+` `=` `diff` ` ` `sum` `=` `sum` `/` `/` `2` ` ` `# Initializing the matrix` ` ` `t ` `=` `[[` `0` `for` `i ` `in` `range` `(` `sum` `+` `1` `)]` ` ` `for` `i ` `in` `range` `(n ` `+` `1` `)]` ` ` `# Number of ways to get sum` ` ` `# using 0 elements is 0` ` ` `for` `j ` `in` `range` `(` `sum` `+` `1` `):` ` ` `t[` `0` `][j] ` `=` `0` ` ` `# Number of ways to get sum 0` ` ` `# using i elements is 1` ` ` `for` `i ` `in` `range` `(n ` `+` `1` `):` ` ` `t[i][` `0` `] ` `=` `1` ` ` `# Traverse the 2D array` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `):` ` ` `for` `j ` `in` `range` `(` `1` `, ` `sum` `+` `1` `):` ` ` ` ` `# If the value is greater` ` ` `# than the sum store the` ` ` `# value of previous state` ` ` `if` `(arr[i ` `-` `1` `] > j):` ` ` `t[i][j] ` `=` `t[i ` `-` `1` `][j]` ` ` `else` `:` ` ` `t[i][j] ` `=` `t[i ` `-` `1` `][j] ` `+` `t[i ` `-` `1` `][j ` `-` `arr[i ` `-` `1` `]]` ` ` `# Return the result` ` ` `return` `t[n][` `sum` `]` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `# Given Input` ` ` `diff, n ` `=` `1` `, ` `4` ` ` `arr ` `=` `[ ` `1` `, ` `1` `, ` `2` `, ` `3` `]` ` ` `# Function Call` ` ` `print` `(countSubset(arr, n, diff))` `# This code is contributed by mohit kumar 29` |

## C#

`// C# program for the above approach` `using` `System;` `public` `class` `GFG` `{` ` ` `// Function to count the number of ways to divide` ` ` `// the array into two subsets and such that the` ` ` `// difference between their sums is equal to diff` ` ` `static` `int` `countSubset(` `int` `[]arr, ` `int` `n, ` `int` `diff)` ` ` `{` ` ` ` ` `// Store the sum of the set S1` ` ` `int` `sum = 0;` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `sum += arr[i];` ` ` `sum += diff;` ` ` `sum = sum / 2;` ` ` ` ` `// Initializing the matrix` ` ` `int` `[,]t = ` `new` `int` `[n + 1, sum + 1];` ` ` ` ` `// Number of ways to get sum` ` ` `// using 0 elements is 0` ` ` `for` `(` `int` `j = 0; j <= sum; j++)` ` ` `t[0,j] = 0;` ` ` ` ` `// Number of ways to get sum 0` ` ` `// using i elements is 1` ` ` `for` `(` `int` `i = 0; i <= n; i++)` ` ` `t[i,0] = 1;` ` ` ` ` `// Traverse the 2D array` ` ` `for` `(` `int` `i = 1; i <= n; i++) {` ` ` `for` `(` `int` `j = 1; j <= sum; j++) {` ` ` ` ` `// If the value is greater` ` ` `// than the sum store the` ` ` `// value of previous state` ` ` `if` `(arr[i - 1] > j)` ` ` `t[i,j] = t[i - 1,j];` ` ` ` ` `else` `{` ` ` `t[i,j] = t[i - 1,j]` ` ` `+ t[i - 1,j - arr[i - 1]];` ` ` `}` ` ` `}` ` ` `}` ` ` ` ` `// Return the result` ` ` `return` `t[n,sum];` ` ` `}` ` ` ` ` `// Driver Code` ` ` `public` `static` `void` `Main(` `string` `[] args)` ` ` `{` ` ` ` ` `// Given Input` ` ` `int` `diff = 1, n = 4;` ` ` `int` `[]arr = { 1, 1, 2, 3 };` ` ` ` ` `// Function Call` ` ` `Console.Write(countSubset(arr, n, diff));` ` ` `}` `}` `// This code is contributed by AnkThon` |

## Javascript

`<script>` `// JavaScript program for the above approach` `// Function to count the number of ways to divide` `// the array into two subsets and such that the` `// difference between their sums is equal to diff` `function` `countSubset(arr, n, diff)` `{` ` ` `// Store the sum of the set S1` ` ` `var` `sum = 0;` ` ` `for` `(` `var` `i = 0; i < n; i++){` ` ` `sum += arr[i];` ` ` `}` ` ` `sum += diff;` ` ` `sum = sum / 2;` ` ` `// Initializing the matrix` ` ` `//int t[n + 1][sum + 1];` ` ` `var` `t = ` `new` `Array(n + 1);` ` ` ` ` `// Loop to create 2D array using 1D array` ` ` `for` `(` `var` `i = 0; i < t.length; i++) {` ` ` `t[i] = ` `new` `Array(sum + 1);` ` ` `}` ` ` ` ` `// Loop to initialize 2D array elements.` ` ` `for` `(` `var` `i = 0; i < t.length; i++) {` ` ` `for` `(` `var` `j = 0; j < t[i].length; j++) {` ` ` `t[i][j] = 0;` ` ` `}` ` ` `}` ` ` `// Number of ways to get sum` ` ` `// using 0 elements is 0` ` ` `for` `(` `var` `j = 0; j <= sum; j++)` ` ` `t[0][j] = 0;` ` ` `// Number of ways to get sum 0` ` ` `// using i elements is 1` ` ` `for` `(` `var` `i = 0; i <= n; i++)` ` ` `t[i][0] = 1;` ` ` `// Traverse the 2D array` ` ` `for` `(` `var` `i = 1; i <= n; i++) {` ` ` `for` `(` `var` `j = 1; j <= sum; j++) {` ` ` `// If the value is greater` ` ` `// than the sum store the` ` ` `// value of previous state` ` ` `if` `(arr[i - 1] > j)` ` ` `t[i][j] = t[i - 1][j];` ` ` `else` `{` ` ` `t[i][j] = t[i - 1][j]` ` ` `+ t[i - 1][j - arr[i - 1]];` ` ` `}` ` ` `}` ` ` `}` ` ` `// Return the result` ` ` `return` `t[n][sum];` `}` `// Driver Code` `// Given Input` `var` `diff = 1;` `var` `n = 4;` `var` `arr = [ 1, 1, 2, 3 ];` `// Function Call` `document.write(countSubset(arr, n, diff));` `</script>` |

**Output:**

3

**Time Complexity:** O(S*N), where S = *sum of array elements** + K/2 ***Auxiliary Space:** O(S*N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.