# Number of subsets with sum divisible by m

Given an array of integers, find number of subsequence such that the sum of the subsequence is divisible by m. It is given that sum of array elements is small.

Examples:

Input : arr[] = {1, 2, 3}; m = 3; Output : 3 Subsequence of given set are {1}, {2}, {3}, {1, 2}, {2, 3}, {1, 3} and {1, 2, 3}. And their sums are 1, 2, 3, 3, 5, 4 and 6. Input : arr[] = {1, 2, 3, 4}; m = 2; Output : 7 {2}, {4}, {1, 3}, {2, 4}, {1, 2, 3}, {1, 3, 4} and {1, 2, 3, 4}

A **simple solution **is to generate all possible subsets. For every subset, compute its sum and if sum is multiiple of m, increment result by 1. Time complexity of this approach is **O(2 ^{len})** where len is length of input array.

An **efficient solution **(for small values) is based on Dynamic Programming solution of subset sum problem. We make a 2D array of size sum x n.

`// C++ program which returns the Number of sub sequences ` `// (or subsets) which are divisible by m. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Use Dynamic Programming to find ` `// sum of subsequences. ` `int` `sumSubSequence(vector<` `int` `> arr, ` `int` `len, ` `int` `m) ` `{ ` ` ` `// Find sum pf array elements ` ` ` `int` `sum = 0; ` ` ` `for` `(` `auto` `x : arr) ` ` ` `sum += x; ` ` ` ` ` `// dp[i][j] would be > 0 if arr[0..i-1] has ` ` ` `// a subsequence with sum equal to j. ` ` ` `vector<vector<` `int` `> > dp(len + 1, vector<` `int` `>(sum + 1, 0)); ` ` ` ` ` `// There is always sum equals zero ` ` ` `for` `(` `int` `i = 0; i <= len; i++) ` ` ` `dp[i][0]++; ` ` ` ` ` `// Fill up the dp table ` ` ` `for` `(` `int` `i = 1; i <= len; i++) { ` ` ` ` ` `dp[i][arr[i - 1]]++; ` ` ` `for` `(` `int` `j = 1; j <= sum; j++) { ` ` ` ` ` `if` `(dp[i - 1][j] > 0) { ` ` ` `dp[i][j]++; ` ` ` `dp[i][j + arr[i - 1]]++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Initialize the counter ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `j = 1; j <= sum; j++) ` ` ` ` ` `// Check if the sum exists ` ` ` `if` `(dp[len][j] > 0) ` ` ` ` ` `// check sum is divisible by m ` ` ` `if` `(j % m == 0) ` ` ` `count += dp[len][j]; ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `vector<` `int` `> arr{ 1, 2, 3 }; ` ` ` `int` `m = 3; ` ` ` `int` `len = arr.size(); ` ` ` `cout << sumSubSequence(arr, len, m) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3

Time complexity of the above approach is **O(len*sum)** where len is the size of the array and sum is the sum of all the integers in the array.

## Recommended Posts:

- Number of subsets with product less than k
- Count number of subsets having a particular XOR value
- Number of distinct subsets of a set
- Count number of ways to partition a set into k subsets
- Total number of subsets in which the product of the elements is even
- Minimum number of subsets with distinct elements
- Count minimum number of subsets (or subsequences) with consecutive numbers
- Maximum number of trailing zeros in the product of the subsets of size k
- Number of substrings divisible by 8 but not by 3
- Maximize the number of sum pairs which are divisible by K
- Number of subsequences in a string divisible by n
- Sum of all the elements in an array divisible by a given number K
- Possible to make a divisible by 3 number using all digits in an array
- Count the number of elements in an array which are divisible by k
- Possible cuts of a number such that maximum parts are divisible by 3

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.