# Find the count of subsequences where each element is divisible by K

• Last Updated : 03 May, 2021

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 = 3
Output:
{3}, {6} and {3, 6} are the only valid subsequences.
Input: arr[] = {5, 10, 15, 20, 25}, K = 5
Output: 31

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Approach: Since each of the elements must be divisible by K, total subsequences are equal to 2cnt 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 2cnt – 1.
Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``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);``    ``int` `k = 3;` `    ``cout << countSubSeq(arr, n, k);` `    ``return` `0;``}`

## Java

 `// 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`

## Python3

 `# 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`

## C#

 `// 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`

## Javascript

 ``
Output:
`3`

My Personal Notes arrow_drop_up