Maximum size of subset such that product of all subset elements is a factor of N
Last Updated :
02 Aug, 2022
Given an integer N and an array arr[] having M integers, the task is to find the maximum size of the subset such that the product of all elements of the subset is a factor of N.
Examples:
Input: N = 12, arr[] = {2, 3, 4}
Output: 2
Explanation: 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
Approach: The given problem can be solved using recursion by traversing over all the subsets of the given array arr[] and keeping track of the size of the subsets such that N % (product of subset elements) = 0. Also for the product of the subset elements to be a factor of N, all individual elements of the array arr[] must also be a factor of N. Therefore, the above problem can be solved using the following steps:
- Create a recursive function maximizeSubset(), which calculates the maximum size of the required subset.
- If all the elements of the given array arr[] have been traversed, return 0 which is the base case.
- Iterate over all elements of the array arr[] and if N % arr[i] = 0, include arr[i] in a subset and recursively call for N = N/arr[i] for the remaining array elements.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximizeSubset( int N, int arr[],
int M, int x = 0)
{
if (x == M) {
return 0;
}
int ans = 0;
for ( int i = x; i < M; i++) {
if (N % arr[i] == 0) {
ans = max(
ans, maximizeSubset(
N / arr[i], arr,
M, x + 1)
+ 1);
}
}
return ans;
}
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
import java.util.*;
class GFG {
static int maximizeSubset( int N, int [] arr, int M,
int x)
{
if (x == M) {
return 0 ;
}
int ans = 0 ;
for ( int i = x; i < M; i++) {
if (N % arr[i] == 0 ) {
ans = Math.max(ans,
maximizeSubset(N / arr[i],
arr, M, x + 1 )
+ 1 );
}
}
return ans;
}
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 ));
}
}
|
Python3
def maximizeSubset(N, arr, M, x = 0 ):
if (x = = M):
return 0
ans = 0
for i in range (x, M):
if (N % arr[i] = = 0 ):
ans = max (
ans, maximizeSubset(
N / / arr[i], arr,
M, x + 1 )
+ 1 )
return ans
N = 64
arr = [ 1 , 2 , 4 , 8 , 16 , 32 ]
M = len (arr)
print (maximizeSubset(N, arr, M))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int maximizeSubset( int N, int []arr,
int M, int x)
{
if (x == M) {
return 0;
}
int ans = 0;
for ( int i = x; i < M; i++) {
if (N % arr[i] == 0) {
ans = Math.Max(
ans, maximizeSubset(
N / arr[i], arr,
M, x + 1)
+ 1);
}
}
return ans;
}
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));
}
}
|
Javascript
<script>
function maximizeSubset(N, arr,
M, x = 0)
{
if (x == M) {
return 0;
}
let ans = 0;
for (let i = x; i < M; i++) {
if (N % arr[i] == 0) {
ans = Math.max(
ans, maximizeSubset(
Math.floor(N / arr[i]), arr,
M, x + 1)
+ 1);
}
}
return ans;
}
let N = 64;
let arr = [1, 2, 4, 8, 16, 32];
let M = arr.length;
document.write(maximizeSubset(N, arr, M));
</script>
|
Time Complexity: O(2N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...