
You should return expressions without redundant parentheses, i.e if the returning expression is ‘(a/b)/c’, you should return ‘a/b/c’.
You are given ‘arr’ = [4, 5, 6], Here the possible expressions are ‘4/5/6’, ‘4/(5/ 6)’, whose values are 0.133 and 4.81 respectively, therefore ‘4/(5/6)’ is the answer.
The first line of input contains a single integer ‘T’ representing the number of test cases.
The first line of each test case contains a single integer ‘N’ representing the size of the array.
The second line of each test case contains ‘N’ space-separated integers representing the elements of the array.
For each test case print the string that represents the expression with the maximum value.
1 <= T <= 10
1 <= N <= 10^6
1 <= arr[i] <= 10^9
Time Limit: 1 sec
You do not need to print anything. It has already been taken care of. Just implement the given function.
In this approach, we will try to put the parentheses at every position, so we will divide ‘arr’ array into two parts left and right, both returning their maximum as well as minimum values.
Now If we divide a large number by a smaller number it will give a larger answer.
So to find the solution to an array, divide the maximum of the left side to the minimum for the right side. Divide the array at every possible position and get the maximum possible answer for the array.
To do this we create a class Value, which stores maximum and minimum values in integer and as a string of any subarray.
We also create a recursive function getMaxValue(arr, start, end), to get the Value Object from the subarray of ‘arr’, ‘arr[start: end]’
Algorithm:
In this approach, we will make a simple observation, like the last approach, that if we divide a large number by a smaller number it will give a larger answer.
Therefore we can simply evaluate the expression from the second number to the last and divide the first number by the result of the expression from 2nd number to the last number.
Algorithm: