Product of the maximums of all subsets of an array
Last Updated :
16 May, 2023
Given an array arr[] consisting of N positive integers, the task is to find the product of the maximum of all possible subsets of the given array. Since the product can be very large, print it to modulo (109 + 7).
Examples:
Input: arr[] = {1, 2, 3}
Output:
Explanation:
All possible subsets of the given array with their respective maximum elements are:
- {1}, the maximum element is 1.
- {2}, the maximum element is 2.
- {3}, the maximum element is 3.
- {1, 2}, the maximum element is 2.
- {1, 3}, the maximum element is 3.
- {2, 3}, the maximum element is 3.
- {1, 2, 3}, the maximum element is 3.
The product of all the above maximum element is 1*2*3*2*3*3*3 = 324.
Input: arr[] = {1, 1, 1, 1}
Output: 1
Naive Approach: The simplest approach to solve the given problem is to generate all possible subsets of the given array and find the product of the maximum of all the generated subsets modulo (109 + 7) as the resultant product.
C++
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
long long int maxProduct( int arr[], int n) {
long long int ans = 1;
for ( int i = 0; i < (1 << n); i++) {
long long int maxVal = INT_MIN;
for ( int j = 0; j < n; j++) {
if ((i & (1 << j)) != 0) {
maxVal = max(maxVal, ( long long int )arr[j]);
}
}
if (maxVal != INT_MIN)
ans = (ans * maxVal) % mod;
}
return ans;
}
void printMaxProduct( int arr[], int n) {
long long int ans = maxProduct(arr, n);
cout << ans << endl;
}
int main() {
int arr[] = { 1, 2, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
printMaxProduct(arr, n);
return 0;
}
|
Java
import java.util.*;
public class Main {
static final int mod = 1000000007 ;
static long maxProduct( int [] arr, int n)
{
long ans = 1 ;
for ( int i = 0 ; i < ( 1 << n); i++) {
long maxVal = Integer.MIN_VALUE;
for ( int j = 0 ; j < n; j++) {
if ((i & ( 1 << j)) != 0 ) {
maxVal = Math.max(maxVal, arr[j]);
}
}
if (maxVal != Integer.MIN_VALUE) {
ans = (ans * maxVal) % mod;
}
}
return ans;
}
static void printMaxProduct( int [] arr, int n)
{
long ans = maxProduct(arr, n);
System.out.println(ans);
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 };
int n = arr.length;
printMaxProduct(arr, n);
}
}
|
Python3
MOD = int ( 1e9 + 7 )
def maxProduct(arr, n):
ans = 1
for i in range ( 1 << n):
maxVal = float ( '-inf' )
for j in range (n):
if i & ( 1 << j):
maxVal = max (maxVal, arr[j])
if maxVal ! = float ( '-inf' ):
ans = (ans * maxVal) % MOD
return ans
def printMaxProduct(arr, n):
ans = maxProduct(arr, n)
print (ans)
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 ]
n = len (arr)
printMaxProduct(arr, n)
|
C#
using System;
public class GFG {
static readonly int mod = 1000000007;
static long MaxProduct( int [] arr, int n)
{
long ans = 1;
for ( int i = 0; i < (1 << n); i++) {
long maxVal = int .MinValue;
for ( int j = 0; j < n; j++) {
if ((i & (1 << j)) != 0) {
maxVal = Math.Max(maxVal, arr[j]);
}
}
if (maxVal != int .MinValue) {
ans = (ans * maxVal) % mod;
}
}
return ans;
}
static void PrintMaxProduct( int [] arr, int n)
{
long ans = MaxProduct(arr, n);
Console.WriteLine(ans);
}
public static void Main()
{
int [] arr = { 1, 2, 3 };
int n = arr.Length;
PrintMaxProduct(arr, n);
}
}
|
Javascript
const mod = 1000000007;
function maxProduct(arr, n) {
let ans = 1;
for (let i = 0; i < (1 << n); i++) {
let maxVal = Number.MIN_SAFE_INTEGER;
for (let j = 0; j < n; j++) {
if ((i & (1 << j)) !== 0) {
maxVal = Math.max(maxVal, arr[j]);
}
}
if (maxVal !== Number.MIN_SAFE_INTEGER) {
ans = (ans * maxVal) % mod;
}
}
return ans;
}
function printMaxProduct(arr, n) {
let ans = maxProduct(arr, n);
console.log(ans);
}
let arr = [1, 2, 3];
let n = arr.length;
printMaxProduct(arr, n);
|
Time Complexity: O(N*2N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized based on the following observations:
- The idea is to count the number of times each array element occurs as the maximum element among all possible subsets formed.
- An array element arr[i] is a maximum if and only if all the elements except arr[i] are smaller than or equal to it.
- Therefore, the number of subsets formed by all elements smaller than or equal to each array element arr[i] contributes to the count of subsets having arr[i] as the maximum element.
Follow the steps below to solve the problem:
- Initialize a variable, say maximumProduct as 1 that stores the resultant product of maximum elements of all subsets.
- Sort the given array arr[] in the increasing order.
- Traverse the array from the end using the variable i and perform the following steps:
- Find the number of subsets that are smaller than the current element arr[i] as (2i – 1) and store it in a variable say P.
- Since the array element arr[i] contributes P number of times, therefore multiply the value arr[i], P times to the variable maximumProduct.
- Find the product of the array element with maximumProduct for including all the subsets of size 1.
- After completing the above steps, print the value of maximumProduct as the resultant maximum product.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long maximumProduct( int arr[], int N)
{
long mod = 1000000007;
sort(arr, arr + N);
long power[N + 1];
power[0] = 1;
for ( int i = 1; i <= N; i++) {
power[i] = 2 * power[i - 1];
power[i] %= mod;
}
long result = 1;
for ( int i = N - 1; i > 0; i--) {
long value = (power[i] - 1);
for ( int j = 0; j < value; j++) {
result *= 1LL * arr[i];
result %= mod;
}
}
for ( int i = 0; i < N; i++) {
result *= 1LL * arr[i];
result %= mod;
}
return result;
}
int main()
{
int arr[] = { 1, 2, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << maximumProduct(arr, N);
return 0;
}
|
Java
import java.util.Arrays;
class GFG{
static long maximumProduct( int arr[], int N)
{
long mod = 1000000007 ;
Arrays.sort(arr);
long power[] = new long [N + 1 ];
power[ 0 ] = 1 ;
for ( int i = 1 ; i <= N; i++)
{
power[i] = 2 * power[i - 1 ];
power[i] %= mod;
}
long result = 1 ;
for ( int i = N - 1 ; i > 0 ; i--)
{
long value = (power[i] - 1 );
for ( int j = 0 ; j < value; j++)
{
result *= arr[i];
result %= mod;
}
}
for ( int i = 0 ; i < N; i++)
{
result *= arr[i];
result %= mod;
}
return result;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 };
int N = arr.length;
System.out.println(maximumProduct(arr, N));
}
}
|
Python3
def maximumProduct(arr, N):
mod = 1000000007
arr = sorted (arr)
power = [ 0 ] * (N + 1 )
power[ 0 ] = 1
for i in range ( 1 , N + 1 ):
power[i] = 2 * power[i - 1 ]
power[i] % = mod
result = 1
for i in range (N - 1 , - 1 , - 1 ):
value = (power[i] - 1 )
for j in range (value):
result * = arr[i]
result % = mod
for i in range (N):
result * = arr[i]
result % = mod
return result
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 ]
N = len (arr)
print (maximumProduct(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static long maximumProduct( int []arr, int N)
{
long mod = 1000000007;
Array.Sort(arr);
long []power = new long [N + 1];
power[0] = 1;
for ( int i = 1; i <= N; i++) {
power[i] = 2 * power[i - 1];
power[i] %= mod;
}
long result = 1;
for ( int i = N - 1; i > 0; i--) {
long value = (power[i] - 1);
for ( int j = 0; j < value; j++) {
result *= 1 * arr[i];
result %= mod;
}
}
for ( int i = 0; i < N; i++) {
result *= 1 * arr[i];
result %= mod;
}
return result;
}
public static void Main()
{
int []arr = {1, 2, 3};
int N = arr.Length;
Console.Write(maximumProduct(arr, N));
}
}
|
Javascript
<script>
function maximumProduct(arr, N)
{
let mod = 1000000007;
arr.sort((a, b) => a - b);
let power = new Array(N + 1);
power[0] = 1;
for (let i = 1; i <= N; i++) {
power[i] = 2 * power[i - 1];
power[i] %= mod;
}
let result = 1;
for (let i = N - 1; i > 0; i--) {
let value = (power[i] - 1);
for (let j = 0; j < value; j++) {
result *= 1 * arr[i];
result %= mod;
}
}
for (let i = 0; i < N; i++) {
result *= 1 * arr[i];
result %= mod;
}
return result;
}
let arr = [1, 2, 3 ];
let N = arr.length;
document.write(maximumProduct(arr, N));
</script>
|
Time Complexity: O(N*)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...