Maximum size of subset such that product of all subset elements is a factor of N | Function to find the maximum size of the subset such that the product of subset elements is a factor of N ; Base Case ; Stores maximum size of valid subset ; Traverse the given array ; If N % arr [ i ] = 0 , include arr [ i ] in a subset and recursively call for the remaining array integers ; Return Answer ; Driver Code
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 ) ) ;