数组所有子集的最大值的乘积
给定一个由N个正整数组成的数组arr[] ,任务是找到给定数组的所有可能子集的最大值的乘积。由于产品可能非常大,请将其打印为模(10 9 + 7) 。
例子:
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
朴素方法:解决给定问题的最简单方法是生成给定数组的所有可能子集,并找到所有生成的子集的最大值的乘积模(10 9 + 7)作为结果乘积。
时间复杂度: O(N*2 N )
辅助空间: O(1)
有效的方法:上述方法也可以基于以下观察进行优化:
- 这个想法是计算每个数组元素出现的次数作为所有可能形成的子集中的最大元素。
- 数组元素arr[i]是最大值当且仅当除arr[i]之外的所有元素都小于或等于它。
- 因此,由小于或等于每个数组元素arr[i]的所有元素形成的子集的数量对以arr[i]作为最大元素的子集的计数有贡献。
请按照以下步骤解决问题:
- 初始化一个变量,比如maximumProduct为1 ,它存储所有子集的最大元素的结果乘积。
- 按升序对给定数组arr[]进行排序。
- 使用变量i从末尾遍历数组并执行以下步骤:
- 找到小于当前元素arr[i]的子集的数量为(2 i – 1)并将其存储在变量P中。
- 由于数组元素arr[i]贡献了P次,因此将值arr[i]乘以P次到变量maximumProduct 。
- 找到包含所有大小为 1的子集的数组元素与maximumProduct的乘积。
- 完成上述步骤后,打印maximumProduct的值作为结果的最大产品。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the product of the
// maximum of all possible subsets
long maximumProduct(int arr[], int N)
{
long mod = 1000000007;
// Sort the given array arr[]
sort(arr, arr + N);
// Stores the power of 2
long power[N + 1];
power[0] = 1;
// Calculate the power of 2
for (int i = 1; i <= N; i++) {
power[i] = 2 * power[i - 1];
power[i] %= mod;
}
// Stores the resultant product
long result = 1;
// Traverse the array from the back
for (int i = N - 1; i > 0; i--) {
// Find the value of 2^i - 1
long value = (power[i] - 1);
// Iterate value number of times
for (int j = 0; j < value; j++) {
// Multiply value with
// the result
result *= 1LL * arr[i];
result %= mod;
}
}
// Calculate the product of array
// elements with result to consider
// the subset of size 1
for (int i = 0; i < N; i++) {
result *= 1LL * arr[i];
result %= mod;
}
// Return the resultant product
return result;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << maximumProduct(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.Arrays;
class GFG{
// Function to find the product of the
// maximum of all possible subsets
static long maximumProduct(int arr[], int N)
{
long mod = 1000000007;
// Sort the given array arr[]
Arrays.sort(arr);
// Stores the power of 2
long power[] = new long[N + 1];
power[0] = 1;
// Calculate the power of 2
for(int i = 1; i <= N; i++)
{
power[i] = 2 * power[i - 1];
power[i] %= mod;
}
// Stores the resultant product
long result = 1;
// Traverse the array from the back
for(int i = N - 1; i > 0; i--)
{
// Find the value of 2^i - 1
long value = (power[i] - 1);
// Iterate value number of times
for(int j = 0; j < value; j++)
{
// Multiply value with
// the result
result *= arr[i];
result %= mod;
}
}
// Calculate the product of array
// elements with result to consider
// the subset of size 1
for(int i = 0; i < N; i++)
{
result *= arr[i];
result %= mod;
}
// Return the resultant product
return result;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3 };
int N = arr.length;
System.out.println(maximumProduct(arr, N));
}
}
// This code is contributed by rishavmahato348
Python3
# Python3 program for the above approach
# Function to find the product of the
# maximum of all possible subsets
def maximumProduct(arr, N):
mod = 1000000007
# Sort the given array arr[]
arr = sorted(arr)
# Stores the power of 2
power = [0] * (N + 1)
power[0] = 1
# Calculate the power of 2
for i in range(1, N + 1):
power[i] = 2 * power[i - 1]
power[i] %= mod
# Stores the resultant product
result = 1
# Traverse the array from the back
for i in range(N - 1, -1, -1):
# Find the value of 2^i - 1
value = (power[i] - 1)
# Iterate value number of times
for j in range(value):
# Multiply value with
# the result
result *= arr[i]
result %= mod
# Calculate the product of array
# elements with result to consider
# the subset of size 1
for i in range(N):
result *= arr[i]
result %= mod
# Return the resultant product
return result
# Driver Code
if __name__ == '__main__':
arr = [ 1, 2, 3 ]
N = len(arr)
print(maximumProduct(arr, N))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the product of the
// maximum of all possible subsets
static long maximumProduct(int []arr, int N)
{
long mod = 1000000007;
// Sort the given array arr[]
Array.Sort(arr);
// Stores the power of 2
long []power = new long[N + 1];
power[0] = 1;
// Calculate the power of 2
for (int i = 1; i <= N; i++) {
power[i] = 2 * power[i - 1];
power[i] %= mod;
}
// Stores the resultant product
long result = 1;
// Traverse the array from the back
for (int i = N - 1; i > 0; i--) {
// Find the value of 2^i - 1
long value = (power[i] - 1);
// Iterate value number of times
for (int j = 0; j < value; j++) {
// Multiply value with
// the result
result *= 1 * arr[i];
result %= mod;
}
}
// Calculate the product of array
// elements with result to consider
// the subset of size 1
for (int i = 0; i < N; i++) {
result *= 1 * arr[i];
result %= mod;
}
// Return the resultant product
return result;
}
// Driver Code
public static void Main()
{
int []arr = {1, 2, 3};
int N = arr.Length;
Console.Write(maximumProduct(arr, N));
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出:
324
时间复杂度: O(N*log N)
辅助空间: O(N)