Subsets with sum divisible by m
Last Updated :
28 Apr, 2025
Given an array of size n and an integer m, the task is to find the number of non-empty subsequences such that the sum of the subsequence is divisible by m.
Note:
- The sum of all array elements in small, i.e., it is within integer range.
- m > 0.
Examples:
Input : arr[] = [1, 2, 3, 4], m = 2
Output : 7
Explanation: The subsequences are [1, 3], [1, 3, 4], [1, 2, 3], [1, 2, 3, 4], [2], [2, 4], and [4].
Input : arr[] = [1, 2, 3], m = 3
Output : 3
Explanation: The subsequences are [1, 2], [1, 2, 3], [3].
[Naive Approach] Using Recursion - O(2^n) time and O(n) space
The idea is to recursively generate all the possible subsets. For every subset, compute its sum, and if the sum is multiple of m, increment result by 1. Finally, decrement the result value by 1 as empty subset is also counted in this approach.
Recurrence relation:
- subCount(i, sum, m, arr) = subCount(i+1, sum+arr[i], m, arr) + subCount(i+1, sum, m, arr).
Base Case:
- For i == n, subCount(i, sum, m, arr) = 1 if sum % m == 0, 0 otherwise.
C++
// C++ program to find Number of
// subsets with sum divisible by m
#include <bits/stdc++.h>
using namespace std;
// Recursive function which finds the number of subsequences
// starting from index i and current sum.
int subCountRecur(int i, int sum, int m, vector<int> &arr) {
// Base case: End of array
// Check if sum is divisible by m
if (i == arr.size()) {
return (sum % m == 0)?1:0;
}
// Include current element in subset
int take = subCountRecur(i+1, sum+arr[i], m, arr);
// Exclude current element
int noTake = subCountRecur(i+1, sum, m, arr);
return take + noTake;
}
// Function which finds Number of
// subsets with sum divisible by m
int subCount(vector<int> &arr, int m) {
// Decrement 1 from answer as empty
// subsequence is also counted.
return subCountRecur(0, 0, m, arr) - 1;
}
int main() {
vector<int> arr = {1, 2, 3, 4};
int m = 2;
cout << subCount(arr, m);
return 0;
}
Java
// Java program to find Number of
// subsets with sum divisible by m
import java.util.*;
class GfG {
// Recursive function which finds the number of subsequences
// starting from index i and current sum.
static int subCountRecur(int i, int sum, int m, int[] arr) {
// Base case: End of array
// Check if sum is divisible by m
if (i == arr.length) {
return (sum % m == 0) ? 1 : 0;
}
// Include current element in subset
int take = subCountRecur(i + 1, sum + arr[i], m, arr);
// Exclude current element
int noTake = subCountRecur(i + 1, sum, m, arr);
return take + noTake;
}
// Function which finds Number of
// subsets with sum divisible by m
static int subCount(int[] arr, int m) {
// Decrement 1 from answer as empty
// subsequence is also counted.
return subCountRecur(0, 0, m, arr) - 1;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int m = 2;
System.out.println(subCount(arr, m));
}
}
Python
# Python program to find Number of
# subsets with sum divisible by m
# Recursive function which finds the number of subsequences
# starting from index i and current sum.
def subCountRecur(i, sum, m, arr):
# Base case: End of array
# Check if sum is divisible by m
if i == len(arr):
return 1 if sum % m == 0 else 0
# Include current element in subset
take = subCountRecur(i + 1, sum + arr[i], m, arr)
# Exclude current element
noTake = subCountRecur(i + 1, sum, m, arr)
return take + noTake
# Function which finds Number of
# subsets with sum divisible by m
def subCount(arr, m):
# Decrement 1 from answer as empty
# subsequence is also counted.
return subCountRecur(0, 0, m, arr) - 1
if __name__ == "__main__":
arr = [1, 2, 3, 4]
m = 2
print(subCount(arr, m))
C#
// C# program to find Number of
// subsets with sum divisible by m
using System;
class GfG {
// Recursive function which finds the number of subsequences
// starting from index i and current sum.
static int subCountRecur(int i, int sum, int m, int[] arr) {
// Base case: End of array
// Check if sum is divisible by m
if (i == arr.Length) {
return (sum % m == 0) ? 1 : 0;
}
// Include current element in subset
int take = subCountRecur(i + 1, sum + arr[i], m, arr);
// Exclude current element
int noTake = subCountRecur(i + 1, sum, m, arr);
return take + noTake;
}
// Function which finds Number of
// subsets with sum divisible by m
static int subCount(int[] arr, int m) {
// Decrement 1 from answer as empty
// subsequence is also counted.
return subCountRecur(0, 0, m, arr) - 1;
}
static void Main(string[] args) {
int[] arr = {1, 2, 3, 4};
int m = 2;
Console.WriteLine(subCount(arr, m));
}
}
JavaScript
// JavaScript program to find Number of
// subsets with sum divisible by m
// Recursive function which finds the number of subsequences
// starting from index i and current sum.
function subCountRecur(i, sum, m, arr) {
// Base case: End of array
// Check if sum is divisible by m
if (i === arr.length) {
return (sum % m === 0) ? 1 : 0;
}
// Include current element in subset
let take = subCountRecur(i + 1, sum + arr[i], m, arr);
// Exclude current element
let noTake = subCountRecur(i + 1, sum, m, arr);
return take + noTake;
}
// Function which finds Number of
// subsets with sum divisible by m
function subCount(arr, m) {
// Decrement 1 from answer as empty
// subsequence is also counted.
return subCountRecur(0, 0, m, arr) - 1;
}
let arr = [1, 2, 3, 4];
let m = 2;
console.log(subCount(arr, m));
[Better Approach - 1] Using Top-Down DP (Memoization) – O(sum*n) time and O(sum*n) space
Optimal Substructure: Number of subsequences with sum divisible by m starting at index i and sum depends on the subproblems subCount(i+1, sum+arr[i], m) and subCount(i+1, sum, m). By solving these subproblems, we can determine the result for the current state.
Overlapping Subproblems: Recursive calls often compute the same (i, sum) states multiple times. To avoid recomputation, we store results in a 2D memo table.
- Since recursion changes with
i
and sum
, create a 2D memo table of size n * (sum + 1). memo[i][j]
stores the number of subsequences starting from index i
with current sum j
.- Before computing a state, check if it is already stored in
memo
. If yes, return it. - Subtract 1 from the final result to exclude the empty subsequence.
C++
// C++ program to find Number of
// subsets with sum divisible by m
#include <bits/stdc++.h>
using namespace std;
// Memoized function which finds the number of subsequences
// starting from index i and current sum.
int subCountRecur(int i, int sum, int m, vector<int> &arr,
vector<vector<int>> &memo) {
// Base case: End of array
// Check if sum is divisible by m
if (i == arr.size()) {
return (sum % m == 0) ? 1 : 0;
}
// If this state has already been computed,
// return the stored result
if (memo[i][sum] != -1) {
return memo[i][sum];
}
// Include current element in subset
int take = subCountRecur(i+1, sum+arr[i], m, arr, memo);
// Exclude current element
int noTake = subCountRecur(i+1, sum, m, arr, memo);
// Store and return the result
return memo[i][sum] = take + noTake;
}
// Function which finds Number of
// subsets with sum divisible by m
int subCount(vector<int> &arr, int m) {
int n = arr.size();
// Calculate total sum of array
int totalSum = 0;
for (int num : arr) {
totalSum += num;
}
// Initialize memoization table with -1
vector<vector<int>> memo(arr.size(), vector<int>(totalSum + 1, -1));
// Decrement 1 from answer as empty
// subsequence is also counted.
return subCountRecur(0, 0, m, arr, memo) - 1;
}
int main() {
vector<int> arr = {1, 2, 3, 4};
int m = 2;
cout << subCount(arr, m);
return 0;
}
Java
// Java program to find Number of
// subsets with sum divisible by m
import java.util.*;
class GfG {
// Memoized function which finds the number of subsequences
// starting from index i and current sum.
static int subCountRecur(int i, int sum, int m,
int[] arr, int[][] memo) {
// Base case: End of array
// Check if sum is divisible by m
if (i == arr.length) {
return (sum % m == 0) ? 1 : 0;
}
// If this state has already been computed,
// return the stored result
if (memo[i][sum] != -1) {
return memo[i][sum];
}
// Include current element in subset
int take = subCountRecur(i + 1, sum + arr[i], m, arr, memo);
// Exclude current element
int noTake = subCountRecur(i + 1, sum, m, arr, memo);
// Store and return the result
return memo[i][sum] = take + noTake;
}
// Function which finds Number of
// subsets with sum divisible by m
static int subCount(int[] arr, int m) {
int n = arr.length;
// Calculate total sum of array
int totalSum = 0;
for (int num : arr) {
totalSum += num;
}
// Initialize memoization table with -1
int[][] memo = new int[n][totalSum + 1];
for (int i = 0; i < n; i++) {
Arrays.fill(memo[i], -1);
}
// Decrement 1 from answer as empty
// subsequence is also counted.
return subCountRecur(0, 0, m, arr, memo) - 1;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int m = 2;
System.out.println(subCount(arr, m));
}
}
Python
# Python program to find Number of
# subsets with sum divisible by m
# Memoized function which finds the number of subsequences
# starting from index i and current sum.
def subCountRecur(i, sum, m, arr, memo):
# Base case: End of array
# Check if sum is divisible by m
if i == len(arr):
return 1 if sum % m == 0 else 0
# If this state has already been computed,
# return the stored result
if memo[i][sum] != -1:
return memo[i][sum]
# Include current element in subset
take = subCountRecur(i + 1, sum + arr[i], m, arr, memo)
# Exclude current element
noTake = subCountRecur(i + 1, sum, m, arr, memo)
# Store and return the result
memo[i][sum] = take + noTake
return memo[i][sum]
# Function which finds Number of
# subsets with sum divisible by m
def subCount(arr, m):
n = len(arr)
# Calculate total sum of array
totalSum = sum(arr)
# Initialize memoization table with -1
memo = [[-1 for _ in range(totalSum + 1)] for _ in range(n)]
# Decrement 1 from answer as empty
# subsequence is also counted.
return subCountRecur(0, 0, m, arr, memo) - 1
if __name__ == "__main__":
arr = [1, 2, 3, 4]
m = 2
print(subCount(arr, m))
C#
// C# program to find Number of
// subsets with sum divisible by m
using System;
class GfG {
// Memoized function which finds the number of subsequences
// starting from index i and current sum.
static int subCountRecur(int i, int sum, int m,
int[] arr, int[,] memo) {
// Base case: End of array
// Check if sum is divisible by m
if (i == arr.Length) {
return (sum % m == 0) ? 1 : 0;
}
// If this state has already been computed,
// return the stored result
if (memo[i, sum] != -1) {
return memo[i, sum];
}
// Include current element in subset
int take = subCountRecur(i + 1, sum + arr[i], m, arr, memo);
// Exclude current element
int noTake = subCountRecur(i + 1, sum, m, arr, memo);
// Store and return the result
return memo[i, sum] = take + noTake;
}
// Function which finds Number of
// subsets with sum divisible by m
static int subCount(int[] arr, int m) {
int n = arr.Length;
// Calculate total sum of array
int totalSum = 0;
foreach (int num in arr) {
totalSum += num;
}
// Initialize memoization table with -1
int[,] memo = new int[n, totalSum + 1];
for (int i = 0; i < n; i++) {
for (int j = 0; j <= totalSum; j++) {
memo[i, j] = -1;
}
}
// Decrement 1 from answer as empty
// subsequence is also counted.
return subCountRecur(0, 0, m, arr, memo) - 1;
}
static void Main(string[] args) {
int[] arr = {1, 2, 3, 4};
int m = 2;
Console.WriteLine(subCount(arr, m));
}
}
JavaScript
// JavaScript program to find Number of
// subsets with sum divisible by m
// Memoized function which finds the number of subsequences
// starting from index i and current sum.
function subCountRecur(i, sum, m, arr, memo) {
// Base case: End of array
// Check if sum is divisible by m
if (i === arr.length) {
return (sum % m === 0) ? 1 : 0;
}
// If this state has already been computed,
// return the stored result
if (memo[i][sum] !== -1) {
return memo[i][sum];
}
// Include current element in subset
let take = subCountRecur(i + 1, sum + arr[i], m, arr, memo);
// Exclude current element
let noTake = subCountRecur(i + 1, sum, m, arr, memo);
// Store and return the result
memo[i][sum] = take + noTake;
return memo[i][sum];
}
// Function which finds Number of
// subsets with sum divisible by m
function subCount(arr, m) {
let n = arr.length;
// Calculate total sum of array
let totalSum = arr.reduce((a, b) => a + b, 0);
// Initialize memoization table with -1
let memo = new Array(n).fill(0).map(() => new Array(totalSum + 1).fill(-1));
// Decrement 1 from answer as empty
// subsequence is also counted.
return subCountRecur(0, 0, m, arr, memo) - 1;
}
let arr = [1, 2, 3, 4];
let m = 2;
console.log(subCount(arr, m));
[Better Approach - 2] Using Bottom-Up DP (Tabulation) – O(sum*n) time and O(sum*n) space
The idea is to fill the DP table based on future values. For each index i
and current sum j
, we either include arr[i]
or exclude it to compute the number of subsequences whose sum is divisible by m
. The table is filled in an iterative manner from i = n-1
down to 0
, and for each sum from 0
to total sum
.
The dynamic programming relation is as follows:
- dp[i][j] = dp[i+1][j] + dp[i+1][j + arr[i]]
At the base case i = n
, we set dp[n][j] = 1
if j % m == 0
, else 0.
Finally, subtract 1 from dp[0][0]
to exclude the empty subsequence.
C++
// C++ program to find Number of
// subsets with sum divisible by m
#include <bits/stdc++.h>
using namespace std;
// Function which finds Number of
// subsets with sum divisible by m
int subCount(vector<int> &arr, int m) {
int n = arr.size();
// Calculate total sum of array
int totalSum = 0;
for (int num : arr) {
totalSum += num;
}
vector<vector<int>> dp(n + 1, vector<int>(totalSum + 1, 0));
// Precompute the sums which are divisible
// by m.
for (int j=0; j<=totalSum; j++) {
if (j%m == 0) dp[n][j] = 1;
}
// Current index
for (int i=n-1; i>=0; i--) {
// Current sum
for (int j=0; j<=totalSum; j++) {
dp[i][j] = dp[i+1][j] + dp[i+1][j+arr[i]];
}
}
return dp[0][0] - 1;
}
int main() {
vector<int> arr = {1, 2, 3, 4};
int m = 2;
cout << subCount(arr, m);
return 0;
}
Java
// Java program to find Number of
// subsets with sum divisible by m
import java.util.*;
class Main {
// Function which finds Number of
// subsets with sum divisible by m
static int subCount(int[] arr, int m) {
int n = arr.length;
// Calculate total sum of array
int totalSum = 0;
for (int num : arr) {
totalSum += num;
}
int[][] dp = new int[n + 1][totalSum + 1];
// Precompute the sums which are divisible
// by m.
for (int j = 0; j <= totalSum; j++) {
if (j % m == 0) dp[n][j] = 1;
}
// Current index
for (int i = n - 1; i >= 0; i--) {
// Current sum
for (int j = 0; j <= totalSum; j++) {
// Exclude arr[i]
dp[i][j] = dp[i + 1][j];
// Include arr[i]
if (j + arr[i] <= totalSum) {
dp[i][j] += dp[i + 1][j + arr[i]];
}
}
}
return dp[0][0] - 1;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int m = 2;
System.out.println(subCount(arr, m));
}
}
Python
# Python program to find Number of
# subsets with sum divisible by m
# Function which finds Number of
# subsets with sum divisible by m
def subCount(arr, m):
n = len(arr)
# Calculate total sum of array
totalSum = 0
for num in arr:
totalSum += num
dp = [[0] * (totalSum + 1) for _ in range(n + 1)]
# Precompute the sums which are divisible
# by m.
for j in range(totalSum + 1):
if j % m == 0:
dp[n][j] = 1
# Current index
for i in range(n - 1, -1, -1):
# Current sum
for j in range(totalSum + 1):
# Exclude arr[i]
dp[i][j] = dp[i + 1][j]
# Include arr[i]
if j + arr[i] <= totalSum:
dp[i][j] += dp[i + 1][j + arr[i]]
return dp[0][0] - 1
if __name__ == "__main__":
arr = [1, 2, 3, 4]
m = 2
print(subCount(arr, m))
C#
// C# program to find Number of
// subsets with sum divisible by m
using System;
class GfG {
// Function which finds Number of
// subsets with sum divisible by m
static int subCount(int[] arr, int m) {
int n = arr.Length;
// Calculate total sum of array
int totalSum = 0;
foreach (int num in arr) {
totalSum += num;
}
int[,] dp = new int[n + 1, totalSum + 1];
// Precompute the sums which are divisible
// by m.
for (int j = 0; j <= totalSum; j++) {
if (j % m == 0) dp[n, j] = 1;
}
// Current index
for (int i = n - 1; i >= 0; i--) {
// Current sum
for (int j = 0; j <= totalSum; j++) {
// Exclude arr[i]
dp[i, j] = dp[i + 1, j];
// Include arr[i]
if (j + arr[i] <= totalSum) {
dp[i, j] += dp[i + 1, j + arr[i]];
}
}
}
return dp[0, 0] - 1;
}
static void Main(string[] args) {
int[] arr = {1, 2, 3, 4};
int m = 2;
Console.WriteLine(subCount(arr, m));
}
}
JavaScript
// JavaScript program to find Number of
// subsets with sum divisible by m
// Function which finds Number of
// subsets with sum divisible by m
function subCount(arr, m) {
let n = arr.length;
// Calculate total sum of array
let totalSum = 0;
for (let num of arr) {
totalSum += num;
}
let dp = new Array(n + 1).fill(0).map(() => new Array(totalSum + 1).fill(0));
// Precompute the sums which are divisible
// by m.
for (let j = 0; j <= totalSum; j++) {
if (j % m === 0) dp[n][j] = 1;
}
// Current index
for (let i = n - 1; i >= 0; i--) {
// Current sum
for (let j = 0; j <= totalSum; j++) {
// Exclude arr[i]
dp[i][j] = dp[i + 1][j];
// Include arr[i]
if (j + arr[i] <= totalSum) {
dp[i][j] += dp[i + 1][j + arr[i]];
}
}
}
return dp[0][0] - 1;
}
let arr = [1, 2, 3, 4];
let m = 2;
console.log(subCount(arr, m));
[Expected Approach - 1] Using Space Optimized DP – O(sum*n) time and O(sum) space
In previous approach of dynamic programming we have derived the relation between states as given below:
- dp[i][j] = dp[i+1][j] + dp[i+1][j + arr[i]]
If we observe carefully, for calculating current dp[i][j]
state we only need values from the next row dp[i+1][...]
. Hence, we can optimize space by using a single 1D array and updating it in reverse to simulate the row transitions.
C++
// C++ program to find Number of
// subsets with sum divisible by m
#include <bits/stdc++.h>
using namespace std;
// Function which finds Number of
// subsets with sum divisible by m
int subCount(vector<int> &arr, int m) {
int n = arr.size();
// Calculate total sum of array
int totalSum = 0;
for (int num : arr) {
totalSum += num;
}
// dp array for tabulation
vector<int> dp(totalSum + 1, 0);
// Base case: sums which are divisible by m
for (int j = 0; j <= totalSum; j++) {
if (j % m == 0) dp[j] = 1;
}
// Process each element in the array
for (int i = n - 1; i >= 0; i--) {
// Current sum
for (int j = 0; j<=totalSum; j++) {
// Include arr[i] if possible
if (j + arr[i] <= totalSum) {
dp[j] += dp[j + arr[i]];
}
}
}
return dp[0] - 1;
}
int main() {
vector<int> arr = {1, 2, 3, 4};
int m = 2;
cout << subCount(arr, m);
return 0;
}
Java
// Java program to find Number of
// subsets with sum divisible by m
import java.util.*;
class GfG {
// Function which finds Number of
// subsets with sum divisible by m
static int subCount(int[] arr, int m) {
int n = arr.length;
// Calculate total sum of array
int totalSum = 0;
for (int num : arr) {
totalSum += num;
}
// dp array for tabulation
int[] dp = new int[totalSum + 1];
// Base case: sums which are divisible by m
for (int j = 0; j <= totalSum; j++) {
if (j % m == 0) dp[j] = 1;
}
// Process each element in the array
for (int i = n - 1; i >= 0; i--) {
// Current sum
for (int j = 0; j <= totalSum; j++) {
// Include arr[i] if possible
if (j + arr[i] <= totalSum) {
dp[j] += dp[j + arr[i]];
}
}
}
return dp[0] - 1;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int m = 2;
System.out.println(subCount(arr, m));
}
}
Python
# Python program to find Number of
# subsets with sum divisible by m
# Function which finds Number of
# subsets with sum divisible by m
def subCount(arr, m):
n = len(arr)
# Calculate total sum of array
totalSum = 0
for num in arr:
totalSum += num
# dp array for tabulation
dp = [0] * (totalSum + 1)
# Base case: sums which are divisible by m
for j in range(totalSum + 1):
if j % m == 0:
dp[j] = 1
# Process each element in the array
for i in range(n - 1, -1, -1):
# Current sum
for j in range(totalSum + 1):
# Include arr[i] if possible
if j + arr[i] <= totalSum:
dp[j] += dp[j + arr[i]]
return dp[0] - 1
if __name__ == "__main__":
arr = [1, 2, 3, 4]
m = 2
print(subCount(arr, m))
C#
// C# program to find Number of
// subsets with sum divisible by m
using System;
class GfG {
// Function which finds Number of
// subsets with sum divisible by m
static int subCount(int[] arr, int m) {
int n = arr.Length;
// Calculate total sum of array
int totalSum = 0;
foreach (int num in arr) {
totalSum += num;
}
// dp array for tabulation
int[] dp = new int[totalSum + 1];
// Base case: sums which are divisible by m
for (int j = 0; j <= totalSum; j++) {
if (j % m == 0) dp[j] = 1;
}
// Process each element in the array
for (int i = n - 1; i >= 0; i--) {
// Current sum
for (int j = 0; j <= totalSum; j++) {
// Include arr[i] if possible
if (j + arr[i] <= totalSum) {
dp[j] += dp[j + arr[i]];
}
}
}
return dp[0] - 1;
}
static void Main(string[] args) {
int[] arr = {1, 2, 3, 4};
int m = 2;
Console.WriteLine(subCount(arr, m));
}
}
JavaScript
// JavaScript program to find Number of
// subsets with sum divisible by m
// Function which finds Number of
// subsets with sum divisible by m
function subCount(arr, m) {
let n = arr.length;
// Calculate total sum of array
let totalSum = 0;
for (let num of arr) {
totalSum += num;
}
// dp array for tabulation
let dp = new Array(totalSum + 1).fill(0);
// Base case: sums which are divisible by m
for (let j = 0; j <= totalSum; j++) {
if (j % m === 0) dp[j] = 1;
}
// Process each element in the array
for (let i = n - 1; i >= 0; i--) {
// Current sum
for (let j = 0; j <= totalSum; j++) {
// Include arr[i] if possible
if (j + arr[i] <= totalSum) {
dp[j] += dp[j + arr[i]];
}
}
}
return dp[0] - 1;
}
let arr = [1, 2, 3, 4];
let m = 2;
console.log(subCount(arr, m));
[Expected Approach - 2] Further Space Optimization - O(sum*n) time and O(m) space
The idea is to reduce the state space by observing that we only care whether a subsequence sum is divisible by m
, so instead of tracking the exact sum, we can track the remainder of the sum modulo m
. We define dp[i][curr]
as the number of subsequences starting from index i
with a current sum having remainder curr
modulo m
. The recurrence becomes dp[i][curr] = dp[i + 1][(curr + arr[i]) % m] + dp[i + 1][curr]
, representing the choices to include or exclude arr[i]
. This allows us to reduce the DP table size from O(n × sum) to O(n × m), optimizing both time and space.
Refer to Number of subsets with sum divisible by M | Set 2 for detailed explanation and code.
Related Articles:
Similar Reads
Subset Sum Problem
Given an array arr[] of non-negative integers and a value sum, the task is to check if there is a subset of the given array whose sum is equal to the given sum. Examples: Input: arr[] = [3, 34, 4, 12, 5, 2], sum = 9Output: TrueExplanation: There is a subset (4, 5) with sum 9.Input: arr[] = [3, 34, 4
15+ min read
Subset sum in Different languages
Python Program for Subset Sum Problem | DP-25
Write a Python program for a given set of non-negative integers and a value sum, the task is to check if there is a subset of the given set whose sum is equal to the given sum. Examples: Input: set[] = {3, 34, 4, 12, 5, 2}, sum = 9Output: TrueExplanation: There is a subset (4, 5) with sum 9. Input:
7 min read
Java Program for Subset Sum Problem | DP-25
Write a Java program for a given set of non-negative integers and a value sum, the task is to check if there is a subset of the given set whose sum is equal to the given sum. Examples: Input: set[] = {3, 34, 4, 12, 5, 2}, sum = 9Output: TrueExplanation: There is a subset (4, 5) with sum 9. Input: se
8 min read
C Program for Subset Sum Problem | DP-25
Write a C program for a given set of non-negative integers and a value sum, the task is to check if there is a subset of the given set whose sum is equal to the given sum. Examples: Input: set[] = {3, 34, 4, 12, 5, 2}, sum = 9Output: TrueExplanation: There is a subset (4, 5) with sum 9. Input: set[]
8 min read
PHP Program for Subset Sum Problem | DP-25
Write a PHP program for a given set of non-negative integers and a value sum, the task is to check if there is a subset of the given set whose sum is equal to the given sum. Examples: Input: set[] = {3, 34, 4, 12, 5, 2}, sum = 9Output: TrueExplanation: There is a subset (4, 5) with sum 9. Input: set
7 min read
C# Program for Subset Sum Problem | DP-25
Write a C# program for a given set of non-negative integers and a value sum, the task is to check if there is a subset of the given set whose sum is equal to the given sum. Examples: Input: set[] = {3, 34, 4, 12, 5, 2}, sum = 9Output: TrueExplanation: There is a subset (4, 5) with sum 9. Input: set[
8 min read
Subset Sum Problem using Backtracking
Given a set[] of non-negative integers and a value sum, the task is to print the subset of the given set whose sum is equal to the given sum. Examples:Â Input:Â set[] = {1,2,1}, sum = 3Output:Â [1,2],[2,1]Explanation:Â There are subsets [1,2],[2,1] with sum 3. Input:Â set[] = {3, 34, 4, 12, 5, 2}, sum =
8 min read
Print all subsets with given sum
Given an array arr[] of non-negative integers and an integer target. The task is to print all subsets of the array whose sum is equal to the given target. Note: If no subset has a sum equal to target, print -1.Examples:Input: arr[] = [5, 2, 3, 10, 6, 8], target = 10Output: [ [5, 2, 3], [2, 8], [10]
15+ min read
Subset Sum Problem in O(sum) space
Given an array of non-negative integers and a value sum, determine if there is a subset of the given set with sum equal to given sum. Examples: Input: arr[] = {4, 1, 10, 12, 5, 2}, sum = 9Output: TRUEExplanation: {4, 5} is a subset with sum 9. Input: arr[] = {1, 8, 2, 5}, sum = 4Output: FALSE Explan
13 min read
Subset Sum is NP Complete
Prerequisite: NP-Completeness, Subset Sum Problem Subset Sum Problem: Given N non-negative integers a1...aN and a target sum K, the task is to decide if there is a subset having a sum equal to K. Explanation: An instance of the problem is an input specified to the problem. An instance of the subset
5 min read
Minimum Subset sum difference problem with Subset partitioning
Given a set of N integers with up to 40 elements, the task is to partition the set into two subsets of equal size (or the closest possible), such that the difference between the sums of the subsets is minimized. If the size of the set is odd, one subset will have one more element than the other. If
13 min read
Maximum subset sum such that no two elements in set have same digit in them
Given an array of N elements. Find the subset of elements which has maximum sum such that no two elements in the subset has common digit present in them.Examples: Input : array[] = {22, 132, 4, 45, 12, 223} Output : 268 Maximum Sum Subset will be = {45, 223} . All possible digits are present except
12 min read
Find all distinct subset (or subsequence) sums of an array
Given an array arr[] of size n, the task is to find a distinct sum that can be generated from the subsets of the given sets and return them in increasing order. It is given that the sum of array elements is small.Examples: Input: arr[] = [1, 2]Output: [0, 1, 2, 3]Explanation: Four distinct sums can
15+ min read
Subset sum problem where Array sum is at most N
Given an array arr[] of size N such that the sum of all the array elements does not exceed N, and array queries[] containing Q queries. For each query, the task is to find if there is a subset of the array whose sum is the same as queries[i]. Examples: Input: arr[] = {1, 0, 0, 0, 0, 2, 3}, queries[]
10 min read