Given an array of integers, arr[] of size N (<=16), the task is to partition the array into 2 parts such that the maximum product of the 2 halves is minimized. Find the minimized maximum product of the half. If the array is empty, print -1.
Examples:
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.
Approach: Since the value of N is less than 16 the problem can be solved using bit masking as multiply all the numbers which are at set bits position and put it into one side similarly multiply all the unset bits position and store it in another half find the maximum of those and store it in a set and at last return first element of the set.
Follow the steps below to solve the problem:
- Initialize a set st[] to store the possible answers in order.
- Iterate over the range [0, 2N) using the variable i and perform the following tasks:
- After performing the above steps, print the first value of the set as the answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void findMinimum(vector< int >& arr, int N)
{
set< int > st;
for ( int i = 0; i < (1 << N); i++) {
int product1 = 1, product2 = 1;
for ( int j = 0; j < N; j++) {
if (i & (1 << j)) {
product1 = product1 * arr[j];
}
else {
product2 = product2 * arr[j];
}
}
st.insert(max(product1, product2));
}
cout << *st.begin() << "\n" ;
}
int main()
{
vector< int > arr = { 3, 5, 7 };
int N = arr.size();
findMinimum(arr, N);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static void findMinimum( int []arr, int N)
{
HashSet<Integer> st = new HashSet<Integer>();
for ( int i = 0 ; i < ( 1 << N); i++) {
int product1 = 1 , product2 = 1 ;
for ( int j = 0 ; j < N; j++) {
if ((i & ( 1 << j)) == 0 ) {
product1 = product1 * arr[j];
}
else {
product2 = product2 * arr[j];
}
}
st.add(Math.max(product1, product2));
}
int ans = 0 ;
for ( int x : st) {
ans = x;
}
System.out.print(ans);
}
public static void main(String args[])
{
int []arr = { 3 , 5 , 7 };
int N = arr.length;
findMinimum(arr, N);
}
}
|
Python3
def findMinimum(arr, N):
st = set ([])
for i in range (( 1 << N)):
product1 = 1
product2 = 1
for j in range (N):
if (i & ( 1 << j)):
product1 = product1 * arr[j]
else :
product2 = product2 * arr[j]
st.add( max (product1, product2))
print ( list (st)[ - 1 ])
if __name__ = = "__main__" :
arr = [ 3 , 5 , 7 ]
N = len (arr)
findMinimum(arr, N)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
static void findMinimum( int []arr, int N)
{
HashSet< int > st = new HashSet< int >();
for ( int i = 0; i < (1 << N); i++) {
int product1 = 1, product2 = 1;
for ( int j = 0; j < N; j++) {
if ((i & (1 << j)) == 0) {
product1 = product1 * arr[j];
}
else {
product2 = product2 * arr[j];
}
}
st.Add(Math.Max(product1, product2));
}
int ans = 0;
foreach ( int x in st) {
ans = x;
}
Console.Write(ans);
}
public static void Main()
{
int []arr = { 3, 5, 7 };
int N = arr.Length;
findMinimum(arr, N);
}
}
|
Javascript
<script>
function findMinimum(arr, N) {
let st = new Set();
for (let i = 0; i < (1 << N); i++) {
let product1 = 1;
let product2 = 1;
for (let j = 0; j < N; j++) {
if (i & (1 << j)) {
product1 = product1 * arr[j];
}
else {
product2 = product2 * arr[j];
}
}
st.add(Math.max(product1, product2));
}
const last = [...st][st.size - 1];
document.write(last);
}
let arr = [3, 5, 7];
let N = arr.length;
findMinimum(arr, N);
</script>
|
Time complexity: O((2^N)*(N*log(N)))
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!