Number of subsets with sum divisible by M | Set 2

13 May, 2021
Given an array arr[] of N integers 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.
Examples:  
Input: arr[] = {1, 2, 3}, M = 1 Output: 7 Number of non-empty subsets of this array are 7. Since m = 1, m will divide all the possible subsets.Input: arr[] = {1, 2, 3}, M = 2 Output: 3  
Approach: A dynamic programming-based approach with O(N * SUM) time complexity where N is the length of the array and SUM is the sum of all the array elements has been discussed in this article.In this article, an improvement over the previous approach will be discussed. Instead of the sum as one of the states of DP, (sum % m) will be used as one of the states of the DP as it is sufficient. Thus, the time complexity boils down to O(m * N).Recurrence relation: 
dp[i][curr] = dp[i + 1][(curr + arr[i]) % m] + dp[i + 1][curr]  
Let’s define the states now, dp[i][curr] simply means number of subsets of the sub-array arr[i...N-1] such that (sum of its element + curr) % m = 0.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;#define maxN 20#define maxM 10 // To store the states of DPint dp[maxN][maxM];bool v[maxN][maxM]; // Function to find the required countint findCnt(int* arr, int i, int curr, int n, int m){    // Base case    if (i == n) {        if (curr == 0)            return 1;        else            return 0;    }     // If the state has been solved before    // return the value of the state    if (v[i][curr])        return dp[i][curr];     // Setting the state as solved    v[i][curr] = 1;     // Recurrence relation    return dp[i][curr] = findCnt(arr, i + 1,                                 curr, n, m)                         + findCnt(arr, i + 1,                                   (curr + arr[i]) % m,                                   n, m);} // Driver codeint main(){    int arr[] = { 3, 3, 3, 3 };    int n = sizeof(arr) / sizeof(int);    int m = 6;     cout << findCnt(arr, 0, 0, n, m) - 1;     return 0;}
// Java implementation of the approachclass GFG{static int maxN = 20;static int maxM = 10; // To store the states of DPstatic int [][]dp = new int[maxN][maxM];static boolean [][]v = new boolean[maxN][maxM]; // Function to find the required countstatic int findCnt(int[] arr, int i,                   int curr, int n, int m){    // Base case    if (i == n)    {        if (curr == 0)            return 1;        else            return 0;    }     // If the state has been solved before    // return the value of the state    if (v[i][curr])        return dp[i][curr];     // Setting the state as solved    v[i][curr] = true;     // Recurrence relation    return dp[i][curr] = findCnt(arr, i + 1,                                 curr, n, m) +                         findCnt(arr, i + 1,                                (curr + arr[i]) % m,                                 n, m);} // Driver codepublic static void main(String[] args){    int arr[] = { 3, 3, 3, 3 };    int n = arr.length;    int m = 6;     System.out.println(findCnt(arr, 0, 0, n, m) - 1);}} // This code is contributed by 29AjayKumar
# Python3 implementation of the approachmaxN = 20maxM = 10 # To store the states of DPdp = [[0 for i in range(maxN)]         for i in range(maxM)]v = [[0 for i in range(maxN)]        for i in range(maxM)] # Function to find the required countdef findCnt(arr, i, curr, n, m):         # Base case    if (i == n):        if (curr == 0):            return 1        else:            return 0     # If the state has been solved before    # return the value of the state    if (v[i][curr]):        return dp[i][curr]     # Setting the state as solved    v[i][curr] = 1     # Recurrence relation    dp[i][curr] = findCnt(arr, i + 1,                          curr, n, m) + \                  findCnt(arr, i + 1,                         (curr + arr[i]) % m, n, m)    return dp[i][curr] # Driver codearr = [3, 3, 3, 3]n = len(arr)m = 6 print(findCnt(arr, 0, 0, n, m) - 1) # This code is contributed by Mohit Kumar
// C# implementation of the approachusing System; class GFG{    static int maxN = 20;    static int maxM = 10;         // To store the states of DP    static int [,]dp = new int[maxN, maxM];    static bool [,]v = new bool[maxN, maxM];         // Function to find the required count    static int findCnt(int[] arr, int i,                       int curr, int n, int m)    {        // Base case        if (i == n)        {            if (curr == 0)                return 1;            else                return 0;        }             // If the state has been solved before        // return the value of the state        if (v[i, curr])            return dp[i, curr];             // Setting the state as solved        v[i, curr] = true;             // Recurrence relation        return dp[i, curr] = findCnt(arr, i + 1,                                     curr, n, m) +                             findCnt(arr, i + 1,                                    (curr + arr[i]) % m, n, m);    }         // Driver code    public static void Main()    {        int []arr = { 3, 3, 3, 3 };        int n = arr.Length;        int m = 6;             Console.WriteLine(findCnt(arr, 0, 0, n, m) - 1);    }} // This code is contributed by kanugargng
<script> // Javascript implementation of the approachvar maxN = 20var maxM = 10 // To store the states of DPvar dp = Array.from(Array(maxN), () => Array(maxM));var v = Array.from(Array(maxN), () => Array(maxM)); // Function to find the required countfunction findCnt(arr, i, curr, n, m){    // Base case    if (i == n) {        if (curr == 0)            return 1;        else            return 0;    }     // If the state has been solved before    // return the value of the state    if (v[i][curr])        return dp[i][curr];     // Setting the state as solved    v[i][curr] = 1;     // Recurrence relation    dp[i][curr] = findCnt(arr, i + 1,                                 curr, n, m)                         + findCnt(arr, i + 1,                                   (curr + arr[i]) % m,                                   n, m);     return dp[i][curr];} // Driver codevar arr = [3, 3, 3, 3];var n = arr.length;var m = 6;document.write( findCnt(arr, 0, 0, n, m) - 1); </script>
7
 
mohit kumar 29
29AjayKumar
kanugargng
itsok
divisibility
subset
Arrays
Combinatorial
Dynamic Programming
Arrays
Dynamic Programming
subset
Combinatorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Search, insert and delete in an unsorted array
Window Sliding Technique
Chocolate Distribution Problem
Find duplicates in O(n) time and O(1) extra space | Set 1
Write a program to print all permutations of a given string
Permutation and Combination in Python
Factorial of a large number
Count of subsets with sum equal to X
itertools.combinations() module in Python to print all possible combinations