将阵列分成两部分,使最大乘积最小化
给定一个大小为N (<=16)的整数数组arr[] ,任务是将数组分成两部分,以使两部分的最大乘积最小化。找到一半的最小最大乘积。如果数组为空,则打印-1。
例子:
Input: arr[] = {3, 5, 7}
Output: 15
Explanation: The possible partitions are –
-> {5, 7} , {3} – here the products are 35 and 3 and maximum product is 35
-> {3, 7} , {5} – here the products are 21 and 5 and maximum product is 21
-> {5, 3} , {7} – here the products are 15 and 7 and maximum product is 15
-> {5, 7, 3} , {} – here the products are 105 and 0 and maximum product is 105
Out of the maximum product obtained i.e. from 105, 35, 21 and 15, the minimum value is 15 and therefore our required answer is 15.
Input: arr[] = { 10 }
Output: 10
Explanation: Since the array contains single element, the array cannot be further divided. Hence the first half contains 10 and the other half contains no element. Therefore, the answer is 10.
方法:由于N的值小于16 ,因此可以使用位掩码来解决问题,将位于设置位位置的所有数字相乘并将其放入一侧,同样将所有未设置位位置相乘并将其存储在另一半找到其中的最大值并将其存储在一个集合中,最后返回集合的第一个元素。
请按照以下步骤解决问题:
- 初始化一个集合st[]以按顺序存储可能的答案。
- 使用变量i迭代范围[0, 2 N )并执行以下任务:
- 将变量product1和product2初始化为1 。
- 使用变量j迭代范围[0, N)并执行以下任务:
- 如果i & (1 << j)为真,则将值arr[j]乘以变量product1 ,否则乘以变量product2。
- 在集合st[]中插入product1或product2的最大值。
- 执行上述步骤后,打印集合的第一个值作为答案。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum of the
// maximum product of the 2 halves
void findMinimum(vector& arr, int N)
{
// Set to store the possible answers
// in ascending order.
set st;
// Traverse over the all possible
for (int i = 0; i < (1 << N); i++) {
// Variables to find the product
// of set bits at set positions
// and unset bits at unset positions
int product1 = 1, product2 = 1;
// Traverse over the array
for (int j = 0; j < N; j++) {
// Check the condition
if (i & (1 << j)) {
product1 = product1 * arr[j];
}
else {
product2 = product2 * arr[j];
}
}
// Insert the maximum one
st.insert(max(product1, product2));
}
cout << *st.begin() << "\n";
}
// Driver Code
int main()
{
vector arr = { 3, 5, 7 };
int N = arr.size();
findMinimum(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to find the minimum of the
// maximum product of the 2 halves
static void findMinimum(int []arr, int N)
{
// Set to store the possible answers
// in ascending order.
HashSet st = new HashSet();
// Traverse over the all possible
for (int i = 0; i < (1 << N); i++) {
// Variables to find the product
// of set bits at set positions
// and unset bits at unset positions
int product1 = 1, product2 = 1;
// Traverse over the array
for (int j = 0; j < N; j++) {
// Check the condition
if ((i & (1 << j)) == 0) {
product1 = product1 * arr[j];
}
else {
product2 = product2 * arr[j];
}
}
// Insert the maximum one
st.add(Math.max(product1, product2));
}
int ans = 0;
for (int x : st) {
ans = x;
}
System.out.print(ans);
}
// Driver Code
public static void main(String args[])
{
int []arr = { 3, 5, 7 };
int N = arr.length;
findMinimum(arr, N);
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python3 program for the above approach
# Function to find the minimum of the
# maximum product of the 2 halves
def findMinimum(arr, N):
# Set to store the possible answers
# in ascending order.
st = set([])
# Traverse over the all possible
for i in range((1 << N)):
# Variables to find the product
# of set bits at set positions
# and unset bits at unset positions
product1 = 1
product2 = 1
# Traverse over the array
for j in range(N):
# Check the condition
if (i & (1 << j)):
product1 = product1 * arr[j]
else:
product2 = product2 * arr[j]
# Insert the maximum one
st.add(max(product1, product2))
print(list(st)[-1])
# Driver Code
if __name__ == "__main__":
arr = [ 3, 5, 7 ]
N = len(arr)
findMinimum(arr, N)
# This code is contributed by ukasp
C#
// C# program for the above approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
// Function to find the minimum of the
// maximum product of the 2 halves
static void findMinimum(int []arr, int N)
{
// Set to store the possible answers
// in ascending order.
HashSet st = new HashSet();
// Traverse over the all possible
for (int i = 0; i < (1 << N); i++) {
// Variables to find the product
// of set bits at set positions
// and unset bits at unset positions
int product1 = 1, product2 = 1;
// Traverse over the array
for (int j = 0; j < N; j++) {
// Check the condition
if ((i & (1 << j)) == 0) {
product1 = product1 * arr[j];
}
else {
product2 = product2 * arr[j];
}
}
// Insert the maximum one
st.Add(Math.Max(product1, product2));
}
int ans = 0;
foreach (int x in st) {
ans = x;
}
Console.Write(ans);
}
// Driver Code
public static void Main()
{
int []arr = { 3, 5, 7 };
int N = arr.Length;
findMinimum(arr, N);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
15
时间复杂度: O((2^N)*(N*log(N)))
辅助空间: O(N)