📜  计算具有乘积K的数组中的子集

📅  最后修改于: 2021-04-27 17:57:18             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是从给定数组中乘积等于K的所有子集找到计数。

例子:

方法:可使用以下递归关系使用动态编程解决问题:

请按照以下步骤解决问题:

  • 初始化一个二维数组,例如dp [] [] ,以存储上述递归关系的重叠子问题。
  • 使用上述递归关系,计算元素乘积等于K的子集的数量。
  • 最后,打印dp [N – 1] [K]的值

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
#define M 1000
 
// Function to find the count of subsets
// whose product of elements is equal to K
int cntSub(int arr[], int idx,
           int prod, int K, int dp[][M])
{
    // Base Case
    if (idx < 0) {
 
        return (prod == K);
    }
 
    // If an already computed
    // subproblem occurred
    if (dp[idx][prod] != -1) {
 
        return dp[idx][prod];
    }
 
    // Count subsets including idx-th
    // element in the subset
    int X = cntSub(arr, idx - 1,
                   prod * arr[idx], K, dp);
 
    // Count subsets without including
    // idx-th element in the subset
    int Y = cntSub(arr, idx - 1,
                   prod, K, dp);
 
    return dp[idx][prod] = X + Y;
}
 
// Utility function to count subsets in
// an array whose product is equal to K
int UtilcntSub(int arr[], int N, int K)
{
    // dp[i][j]: Stores numberof subsets
    // with product j up to the i-th element
    int dp[N][M];
 
    // Initialize dp[][] to -1
    memset(dp, -1, sizeof(dp));
 
    cout << cntSub(arr, N - 1, 1, K, dp);
}
 
// Driver Code
int main()
{
 
    int arr[] = { 1, 1, 2, 2, 3, 4 };
 
    int K = 4;
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    UtilcntSub(arr, N, K);
}


Java
// Java program to implement
// the above approach
import java.util.*;
   
class GFG{
       
static int M = 1000;
  
// Function to find the count of subsets
// whose product of elements is equal to K
static int cntSub(int arr[], int idx,
                  int prod, int K, int dp[][])
{
     
    // Base Case
    if (idx < 0)
    {
        if (prod == K)
            return 1;
        else
            return 0;
    }
  
    // If an already computed
    // subproblem occurred
    if (dp[idx][prod] != -1)
    {
        return dp[idx][prod];
    }
  
    // Count subsets including idx-th
    // element in the subset
    int X = cntSub(arr, idx - 1,
                   prod * arr[idx], K, dp);
  
    // Count subsets without including
    // idx-th element in the subset
    int Y = cntSub(arr, idx - 1,
                   prod, K, dp);
  
    return dp[idx][prod] = X + Y;
}
  
// Utility function to count subsets in
// an array whose product is equal to K
static void UtilcntSub(int arr[], int N, int K)
{
     
    // dp[i][j]: Stores numberof subsets
    // with product j up to the i-th element
    int[][] dp = new int[N][M];
  
    // Initialize dp[][] to -1
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < M; j++)
        {
            dp[i][j] = -1;
        }
    }
 
    System.out.print(cntSub(arr, N - 1, 1, K, dp));
}
   
// Driver code
public static void main(String[] args)
{
    int[] arr = { 1, 1, 2, 2, 3, 4 };
  
    int K = 4;
  
    int N = arr.length;
  
    UtilcntSub(arr, N, K);
}
}
 
// This code is contributed by code_hunt


Python3
# Python program to implement
# the above approach
M = 1000
 
# Function to find the count of subsets
# whose product of elements is equal to K
def cntSub(arr, idx, prod, K):
    global dp
     
    # Base Case
    if (idx < 0):
        return (prod == K)
 
    # If an already computed
    # subproblem occurred
    if (dp[idx][prod] != -1):
        return dp[idx][prod]
 
    # Count subsets including idx-th
    # element in the subset
    X = cntSub(arr, idx - 1, prod * arr[idx], K)
 
    # Count subsets without including
    # idx-th element in the subset
    Y = cntSub(arr, idx - 1, prod, K)
    dp[idx][prod] = X + Y
    return dp[idx][prod]
 
# Utility function to count subsets in
# an array whose product is equal to K
def UtilcntSub(arr, N, K):
   
    # dp[i][j]: Stores numberof subsets
    # with product j up to the i-th element
    print (cntSub(arr, N - 1, 1, K))
     
# Driver Code
if __name__ == '__main__':
    dp = [[-1 for i in range(1000)] for i in range(1000)]
    arr = [1, 1, 2, 2, 3, 4]
    K = 4
    N = len(arr)
    UtilcntSub(arr, N, K)
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach 
using System;
class GFG
{
  
static int M = 1000;
   
// Function to find the count of subsets
// whose product of elements is equal to K
static int cntSub(int[] arr, int idx,
                  int prod, int K, int[,] dp)
{
      
    // Base Case
    if (idx < 0)
    {
        if (prod == K)
            return 1;
        else
            return 0;
    }
   
    // If an already computed
    // subproblem occurred
    if (dp[idx, prod] != -1)
    {
        return dp[idx, prod];
    }
   
    // Count subsets including idx-th
    // element in the subset
    int X = cntSub(arr, idx - 1,
                   prod * arr[idx], K, dp);
   
    // Count subsets without including
    // idx-th element in the subset
    int Y = cntSub(arr, idx - 1,
                   prod, K, dp);
   
    return dp[idx, prod] = X + Y;
}
   
// Utility function to count subsets in
// an array whose product is equal to K
static void UtilcntSub(int[] arr, int N, int K)
{
      
    // dp[i][j]: Stores numberof subsets
    // with product j up to the i-th element
    int[,] dp = new int[N, M];
   
    // Initialize dp[][] to -1
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < M; j++)
        {
            dp[i, j] = -1;
        }
    }
  
    Console.Write(cntSub(arr, N - 1, 1, K, dp));
}
  
// Driver code
public static void Main()
{
    int[] arr = { 1, 1, 2, 2, 3, 4 };
    int K = 4; 
    int N = arr.Length; 
    UtilcntSub(arr, N, K);
}
}
 
// This code is contributed by susmitakundugoaldanga


Javascript


输出:
8

时间复杂度: O(N * K)
辅助空间: O(N * K)