📜  查找每个元素可被K整除的子序列数

📅  最后修改于: 2021-05-04 22:52:58             🧑  作者: Mango

给定一个数组arr []和一个整数K ,任务是从该数组中找到每个子元素可以被K整除的子序列的总数。

例子:

方法:由于每个元素都必须可被K整除,因此总子序列等于2 cnt ,其中cnt是数组中可被K整除的元素数。请注意,将从结果中减去1 ,以排除空的子序列。因此,最终结果将是2 cnt – 1

下面是上述方法的实现:

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[0]);
    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


输出:
3