Optimal Division
The problem can be solved by understanding the mathematical nature of division. Specifically, we want to maximize the result by minimizing the denominator. To achieve this, we divide the first element by the rest of the elements’ continuous division.
Here’s the code that constructs the required expression:
|
|
Explanation:
- If there’s only one element, we return it as a string.
- If there are only two elements, we return them in the format “num1/num2”.
- If there are more than two elements, we divide the first element by the continuous division of the rest, constructing the expression as “num1/(num2/num3/…/numN)”.
For the given example of nums = [1000,100,10,2]
, the result would be “1000/(100/10/2)”, which evaluates to the maximum value.