Given an array **arr[]** and an integer **x**, the task is to count the number of sub-sets of **arr[]** sum of all of whose sub-sets (individually) is divisible by **x**.

**Examples:**

Input:arr[] = {2, 4, 3, 7}, x = 2

Output:3

All valid sub-sets are {2}, {4} and {2, 4}

{2} => 2 is divisible by 2

{4} => 4 is divisible by 2

{2, 4} => 2, 4 and 6 are all divisible by 2

Input:arr[] = {2, 3, 4, 5}, x = 1

Output:15

**Approach:** To choose a sub-set sum of all of whose sub-sets is divisible by **x**, all the elements of the sub-set must be divisible by **x**. So,

- Count all the element from the array that are divisible by
**x**and store it in a variable**count**. - Now, all possible sub-sets satisfying the condition will be
**2**^{count}– 1

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `#define ll long long int ` `using` `namespace` `std; ` ` ` `// Function to return the count of the required sub-sets ` `ll count(` `int` `arr[], ` `int` `n, ` `int` `x) ` `{ ` ` ` ` ` `// Every element is divisible by 1 ` ` ` `if` `(x == 1) { ` ` ` `ll ans = ` `pow` `(2, n) - 1; ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Count of elements which are divisible by x ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(arr[i] % x == 0) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `ll ans = ` `pow` `(2, count) - 1; ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 2, 4, 3, 5 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `x = 1; ` ` ` `cout << count(arr, n, x) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `solution ` `{ ` ` ` `// Function to return the count of the required sub-sets ` `static` `long` `count(` `int` `arr[], ` `int` `n, ` `int` `x) ` `{ ` ` ` ` ` `// Every element is divisible by 1 ` ` ` `if` `(x == ` `1` `) { ` ` ` `long` `ans = (` `long` `)Math.pow(` `2` `, n) - ` `1` `; ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Count of elements which are divisible by x ` ` ` `int` `count = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `if` `(arr[i] % x == ` `0` `) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `long` `ans = (` `long` `)Math.pow(` `2` `, count) - ` `1` `; ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `[]arr = { ` `2` `, ` `4` `, ` `3` `, ` `5` `}; ` ` ` `int` `n = arr.length; ` ` ` `int` `x = ` `1` `; ` ` ` `System.out.println(count(arr, n, x)); ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count of ` `# the required sub-sets ` `def` `count(arr, n, x) : ` ` ` ` ` `# Every element is divisible by 1 ` ` ` `if` `(x ` `=` `=` `1` `) : ` ` ` `ans ` `=` `pow` `(` `2` `, n) ` `-` `1` ` ` `return` `ans; ` ` ` ` ` `# Count of elements which are ` ` ` `# divisible by x ` ` ` `count ` `=` `0` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `if` `(arr[i] ` `%` `x ` `=` `=` `0` `) : ` ` ` `count ` `+` `=` `1` ` ` ` ` `ans ` `=` `pow` `(` `2` `, count) ` `-` `1` ` ` `return` `ans ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[ ` `2` `, ` `4` `, ` `3` `, ` `5` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` `x ` `=` `1` ` ` `print` `(count(arr, n, x)) ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`//C# implementation of the approach ` ` ` `using` `System; ` ` ` `public` `class` `GFG{ ` ` ` `// Function to return the count of the required sub-sets ` `static` `double` `count(` `int` `[]arr, ` `int` `n, ` `int` `x) ` `{ ` ` ` `double` `ans=0; ` ` ` `// Every element is divisible by 1 ` ` ` `if` `(x == 1) { ` ` ` `ans = (Math.Pow(2, n) - 1); ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Count of elements which are divisible by x ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(arr[i] % x == 0) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `ans = (Math.Pow(2, count) - 1); ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` ` ` ` ` `static` `public` `void` `Main (){ ` ` ` ` ` `int` `[]arr = { 2, 4, 3, 5 }; ` ` ` `int` `n = arr.Length; ` ` ` `int` `x = 1; ` ` ` `Console.WriteLine(count(arr, n, x)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the count of the required sub-sets ` `function` `count_t(` `$arr` `, ` `$n` `, ` `$x` `) ` `{ ` ` ` `// Every element is divisible by 1 ` ` ` `if` `(` `$x` `== 1) { ` ` ` `$ans` `= pow(2, ` `$n` `) - 1; ` ` ` `return` `$ans` `; ` ` ` `} ` ` ` ` ` `// Count of elements which are divisible by x ` ` ` `$count` `= 0; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) { ` ` ` `if` `(` `$arr` `[` `$i` `] % ` `$x` `== 0) ` ` ` `$count` `++; ` ` ` `} ` ` ` ` ` `$ans` `= pow(2, ` `$count` `) - 1; ` ` ` `return` `$ans` `; ` `} ` ` ` `// Driver code ` ` ` ` ` `$arr` `= ` `array` `( 2, 4, 3, 5 ); ` ` ` `$n` `= sizeof(` `$arr` `) / sizeof(` `$arr` `[0]); ` ` ` `$x` `= 1; ` ` ` `echo` `count_t(` `$arr` `, ` `$n` `, ` `$x` `); ` ` ` `#This code is contributed by akt_mit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

15

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:

- Count of indices in an array that satisfy the given condition
- Count index pairs which satisfy the given condition
- Pairs from an array that satisfy the given condition
- Sum of absolute differences of pairs from the given array that satisfy the given condition
- Find the Number of Permutations that satisfy the given condition in an array
- Append two elements to make the array satisfy the given condition
- Choose X elements from A[] and Y elements from B[] which satisfy the given condition
- Count of triplets that satisfy the given equation
- Count of triplets in an array that satisfy the given conditions
- Count of Fibonacci pairs which satisfy the given equation
- Count of unordered pairs (x, y) of Array which satisfy given equation
- Count of Subsets containing only the given value K
- Count of subsets having sum of min and max element less than K
- Count no. of ordered subsets having a particular XOR value
- Count of subsets with sum equal to X
- Count number of subsets having a particular XOR value
- Count of subsets with sum equal to X using Recursion
- Count subsets having distinct even numbers
- Count of subsets not containing adjacent elements
- Count of Subsets of a given Set with element X present in it

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.