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++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define maxN 20 ` `#define maxM 10 ` ` ` `// To store the states of DP ` `int` `dp[maxN][maxM]; ` `bool` `v[maxN][maxM]; ` ` ` `// Function to find the required count ` `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] = 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 code ` `int` `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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` `static` `int` `maxN = ` `20` `; ` `static` `int` `maxM = ` `10` `; ` ` ` `// To store the states of DP ` `static` `int` `[][]dp = ` `new` `int` `[maxN][maxM]; ` `static` `boolean` `[][]v = ` `new` `boolean` `[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(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 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `maxN ` `=` `20` `maxM ` `=` `10` ` ` `# To store the states of DP ` `dp ` `=` `[[` `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 count ` `def` `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 code ` `arr ` `=` `[` `3` `, ` `3` `, ` `3` `, ` `3` `] ` `n ` `=` `len` `(arr) ` `m ` `=` `6` ` ` `print` `(findCnt(arr, ` `0` `, ` `0` `, n, m) ` `-` `1` `) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `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 ` |

*chevron_right*

*filter_none*

**Output:**

7

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.

## Recommended Posts:

- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Sum of subsets of all the subsets of an array | O(3^N)
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Number of subsets with sum divisible by m
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Sum of sum of all subsets of a set formed by first N natural numbers
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Perfect Sum Problem (Print all subsets with given sum)
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Sum of all subsets of a set formed by first n natural numbers
- Sum of bitwise OR of all possible subsets of given set
- Sum of bitwise AND of all possible subsets of given set
- Partition of a set into K subsets with equal sum using BitMask and DP
- Number of subsets with zero sum
- Count number of ways to partition a set into k subsets
- Number of distinct subsets of a set
- Split an array into groups of 3 such that X3 is divisible by X2 and X2 is divisible by X1
- Count number of pairs in array having sum divisible by K | SET 2
- Sum of maximum elements of all subsets

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.