# Split the Array in two parts such that maximum product is minimized

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:

Below is the implementation of the above approach.

## 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<``int``>& arr, ``int` `N)` `{`   `    ``// Set to store the possible answers` `    ``// in ascending order.` `    ``set<``int``> 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<``int``> 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<``int``> st = ``new` `HashSet<``int``>();`   `    ``// 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

 ``

Output

`15`

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!
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://www.geeksforgeeks.org/split-the-array-in-two-parts-such-that-maximum-product-is-minimized/", "fetch_time": 1701215522000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2023-50/segments/1700679100016.39/warc/CC-MAIN-20231128214805-20231129004805-00829.warc.gz", "warc_record_offset": 904929151, "warc_record_length": 53674, "token_count": 2426, "char_count": 7383, "score": 3.703125, "int_score": 4, "crawl": "CC-MAIN-2023-50", "snapshot_type": "latest", "language": "en", "language_score": 0.8202747106552124, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00002-of-00064.parquet"}