Given an array **arr[]** and an integer **K**, the task is to find the total number of subsequences from the array where each element is divisible by **K**.

**Examples:**

Input:arr[] = {1, 2, 3, 6}, K = 3Output:3

{3}, {6} and {3, 6} are the only valid subsequences.

Input:arr[] = {5, 10, 15, 20, 25}, K = 5Output:31

**Approach:** Since each of the element must be divisible by **K**, total subsequences are equal to **2 ^{cnt}** where

**cnt**is the number of elements in the array that are divisible by

**K**.

**Note**that

**1**will be subtracted from the result in order to exclude the empty subsequence. So, the final result will be

**2**.

^{cnt}– 1Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `// Function to return the count ` `// of all valid subsequences ` `int` `countSubSeq(` `int` `arr[], ` `int` `n, ` `int` `k) `
`{ ` ` ` ` ` `// To store the count of elements `
` ` `// which are divisible by k `
` ` `int` `count = 0; `
` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { `
` ` ` ` `// If current element is divisible by `
` ` `// k then increment the count `
` ` `if` `(arr[i] % k == 0) { `
` ` `count++; `
` ` `} `
` ` `} `
` ` ` ` `// Total (2^n - 1) non-empty subsequences `
` ` `// are possible with n element `
` ` `return` `(` `pow` `(2, count) - 1); `
`} ` ` ` `// Driver code ` `int` `main() `
`{ ` ` ` `int` `arr[] = { 1, 2, 3, 6 }; `
` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); `
` ` `int` `k = 3; `
` ` ` ` `cout << countSubSeq(arr, n, k); `
` ` ` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`// Java implementation of the approach ` `import` `java.util.*; `
`class` `GFG `
`{ ` ` ` `// Function to return the count ` `// of all valid subsequences ` `static` `int` `countSubSeq(` `int` `arr[], ` `int` `n, ` `int` `k) `
`{ ` ` ` ` ` `// To store the count of elements `
` ` `// which are divisible by k `
` ` `int` `count = ` `0` `; `
` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) `
` ` `{ `
` ` ` ` `// If current element is divisible by `
` ` `// k then increment the count `
` ` `if` `(arr[i] % k == ` `0` `) `
` ` `{ `
` ` `count++; `
` ` `} `
` ` `} `
` ` ` ` `// Total (2^n - 1) non-empty subsequences `
` ` `// are possible with n element `
` ` `return` `(` `int` `) (Math.pow(` `2` `, count) - ` `1` `); `
`} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) `
`{ ` ` ` `int` `arr[] = { ` `1` `, ` `2` `, ` `3` `, ` `6` `}; `
` ` `int` `n = arr.length; `
` ` `int` `k = ` `3` `; `
` ` ` ` `System.out.println(countSubSeq(arr, n, k)); `
`} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

`# Python3 implementation of the approach ` ` ` `# Function to return the count ` `# of all valid subsequences ` `def` `countSubSeq(arr, n, k) : `
` ` ` ` `# To store the count of elements `
` ` `# which are divisible by k `
` ` `count ` `=` `0` `; `
` ` ` ` `for` `i ` `in` `range` `(n) : `
` ` ` ` `# If current element is divisible by `
` ` `# k then increment the count `
` ` `if` `(arr[i] ` `%` `k ` `=` `=` `0` `) : `
` ` `count ` `+` `=` `1` `; `
` ` ` ` `# Total (2^n - 1) non-empty subsequences `
` ` `# are possible with n element `
` ` `return` `(` `2` `*` `*` `count ` `-` `1` `); `
` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: `
` ` ` ` `arr ` `=` `[ ` `1` `, ` `2` `, ` `3` `, ` `6` `]; `
` ` `n ` `=` `len` `(arr); `
` ` `k ` `=` `3` `; `
` ` ` ` `print` `(countSubSeq(arr, n, k)); `
` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

`// C# implementation of the approach ` `using` `System; `
` ` `class` `GFG `
`{ ` ` ` `// Function to return the count ` `// of all valid subsequences ` `static` `int` `countSubSeq(` `int` `[]arr, ` `int` `n, ` `int` `k) `
`{ ` ` ` ` ` `// To store the count of elements `
` ` `// which are divisible by k `
` ` `int` `count = 0; `
` ` ` ` `for` `(` `int` `i = 0; i < n; i++) `
` ` `{ `
` ` ` ` `// If current element is divisible by `
` ` `// k then increment the count `
` ` `if` `(arr[i] % k == 0) `
` ` `{ `
` ` `count++; `
` ` `} `
` ` `} `
` ` ` ` `// Total (2^n - 1) non-empty subsequences `
` ` `// are possible with n element `
` ` `return` `(` `int` `) (Math.Pow(2, count) - 1); `
`} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) `
`{ ` ` ` `int` `[]arr = { 1, 2, 3, 6 }; `
` ` `int` `n = arr.Length; `
` ` `int` `k = 3; `
` ` ` ` `Console.WriteLine(countSubSeq(arr, n, k)); `
`} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

3

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:

- Sum of product of each element with each element after it
- Form an array of distinct elements with each element as sum of an element from each array
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Number of subsequences in a string divisible by n
- Subsequences of size three in an array whose sum is divisible by m
- Number of subsequences in a given binary string divisible by 2
- Count subsequences which contains both the maximum and minimum array element
- Count of subsequences consisting of the same element
- Count subsequences for every array element in which they are the maximum
- Minimum pair sum operations to make array each element divisible by 4
- 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]
- Count sequences of length K having each term divisible by its preceding term
- Find Array formed by adding each element of given array with largest element in new array to its left
- Find just strictly greater element from first array for each element in second array
- Find permutation of n which is divisible by 3 but not divisible by 6
- Find Surpasser Count of each element in array
- Find the difference of count of equal elements on the right and the left for each element
- Range Query on array whose each element is XOR of index value and previous element
- Replace each element by the difference of the total size of the array and frequency of that element
- Array formed from difference of each element from the largest element in the given array

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.