子集的最大大小,使得所有子集元素的乘积是 N 的因子
给定一个整数N和一个具有M个整数的数组arr[] ,任务是找到子集的最大大小,使得子集的所有元素的乘积是N的因子。
例子:
Input: N = 12, arr[] = {2, 3, 4}
Output: 2
Explaination: The given array 5 subsets such that the product of all elements of the subset is a factor of 12. They are {2}, {3}, {4}, {2, 3} as (2 * 3) = 6, and {3, 4} as (3 * 4) = 12. Therefore, the maximum size of the valid subset is 2.
Input: N = 64, arr[] = {1, 2, 4, 8, 16, 32}
Output: 4
方法:给定问题可以使用递归来解决,方法是遍历给定数组arr[]的所有子集并跟踪子集的大小,使得N%(子集元素的乘积)=0 。此外,要使子集元素的乘积成为N的因数,数组arr[]的所有单个元素也必须是N的因数。因此,可以通过以下步骤解决上述问题:
- 创建一个递归函数MaximizeSubset() ,它计算所需子集的最大大小。
- 如果已遍历给定数组arr[]的所有元素,则返回 0,这是基本情况。
- 遍历数组arr[]的所有元素,如果N % arr[i] = 0 ,则将 arr[i ]包含在子集中,并递归调用剩余数组元素的N = N/arr[i] 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum size of
// the subset such that the product of
// subset elements is a factor of N
int maximizeSubset(int N, int arr[],
int M, int x = 0)
{
// Base Case
if (x == M) {
return 0;
}
// Stores maximum size of valid subset
int ans = 0;
// Traverse the given array
for (int i = x; i < M; i++) {
// If N % arr[i] = 0, include arr[i]
// in a subset and recursively call
// for the remaining array integers
if (N % arr[i] == 0) {
ans = max(
ans, maximizeSubset(
N / arr[i], arr,
M, x + 1)
+ 1);
}
}
// Return Answer
return ans;
}
// Driver Code
int main()
{
int N = 64;
int arr[] = { 1, 2, 4, 8, 16, 32 };
int M = sizeof(arr) / sizeof(arr[0]);
cout << maximizeSubset(N, arr, M);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to find the maximum size of
// the subset such that the product of
// subset elements is a factor of N
static int maximizeSubset(int N, int[] arr, int M,
int x)
{
// Base Case
if (x == M) {
return 0;
}
// Stores maximum size of valid subset
int ans = 0;
// Traverse the given array
for (int i = x; i < M; i++) {
// If N % arr[i] = 0, include arr[i]
// in a subset and recursively call
// for the remaining array integers
if (N % arr[i] == 0) {
ans = Math.max(ans,
maximizeSubset(N / arr[i],
arr, M, x + 1)
+ 1);
}
}
// Return Answer
return ans;
}
// Driver Code
public static void main(String[] args)
{
int N = 64;
int[] arr = { 1, 2, 4, 8, 16, 32 };
int M = arr.length;
System.out.println(maximizeSubset(N, arr, M, 0));
}
}
// This code is contributed by ukasp.
Python3
# Python Program to implement
# the above approach
# Function to find the maximum size of
# the subset such that the product of
# subset elements is a factor of N
def maximizeSubset(N, arr, M, x=0):
# Base Case
if (x == M):
return 0
# Stores maximum size of valid subset
ans = 0
# Traverse the given array
for i in range(x, M):
# If N % arr[i] = 0, include arr[i]
# in a subset and recursively call
# for the remaining array integers
if (N % arr[i] == 0):
ans = max(
ans, maximizeSubset(
N // arr[i], arr,
M, x + 1)
+ 1)
# Return Answer
return ans
# Driver Code
N = 64
arr = [1, 2, 4, 8, 16, 32]
M = len(arr)
print(maximizeSubset(N, arr, M))
# This code is contributed by Saurabh jaiswal
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the maximum size of
// the subset such that the product of
// subset elements is a factor of N
static int maximizeSubset(int N, int []arr,
int M, int x)
{
// Base Case
if (x == M) {
return 0;
}
// Stores maximum size of valid subset
int ans = 0;
// Traverse the given array
for (int i = x; i < M; i++) {
// If N % arr[i] = 0, include arr[i]
// in a subset and recursively call
// for the remaining array integers
if (N % arr[i] == 0) {
ans = Math.Max(
ans, maximizeSubset(
N / arr[i], arr,
M, x + 1)
+ 1);
}
}
// Return Answer
return ans;
}
// Driver Code
public static void Main()
{
int N = 64;
int []arr = { 1, 2, 4, 8, 16, 32 };
int M = arr.Length;
Console.Write(maximizeSubset(N, arr, M,0));
}
}
// This code is contributed by ipg2016107.
Javascript
输出:
4
时间复杂度: O(2 N )
辅助空间: O(1)