Ctrl K

Biggest Division Chain

Medium
The numbers of numbers are written in a row separated by division signs: [6, 3, 4] becomes 6/3/4, evaluated left to right. You may add brackets anywhere to change the order of operations. Find the bracketing that makes the value as large as possible and return that expression as a string: the numbers in their original order, / between them, no spaces. The expression must have no redundant brackets: every pair of brackets has to change the result.
Example 1
numbers=[6, 3, 4] → '6/(3/4)'

6/(3/4) = 8, which beats 6/3/4 = 0.5.

Example 2
numbers=[500, 2, 5, 10] → '500/(2/5/10)'

500/(2/5/10) = 500 · 5 · 10 / 2 = 12,500. Writing 500/((2/5)/10) would add redundant brackets, since division already goes left to right.

Example 3
numbers=[7, 9] → '7/9'

Two numbers allow only one expression, and it needs no brackets.

Constraints
  • 1 ≤ len(numbers) ≤ 10
  • 2 ≤ numbers[i] ≤ 1000
  • The optimal expression is unique
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNth Number Built From Given Primes MediumChain of Multiples MediumBest Rotation for a Weighted Sum MediumDuel for the End Cards MediumFour-Corner Rectangles Medium
loading Python…
Input
numbers=[6, 3, 4]
Expected
'6/(3/4)'

Run checks the first 3 tests in your browser. Submit checks all 98 tests on the server, hidden ones included.