I have the following programming question:
Given as input an array of integer lengths with each element denoting the length of the rope required, find the minimum length of the original rope given that at each step, you can only half the length of a rope and the length of each rope must be an integer. Output -1 if no such rope exists.
Additional information on "halving" a rope of length x:
- if x is divisible by 2, then the two resulting ropes would be of length x/2
- otherwise, the two resulting ropes would be of length floor(x/2) and ceiling(x/2)
For example, if I require [3, 5, 2], then the minimum size rope I would require is 10 since '10' can be split into 2 '5's and one of the remaining '5' can be split into '3' and '2'. Then, I would end up with exactly what I require [3, 5, 2]. It is also allowed to end up with excessive rope that is not required.
I am provided with a function that can determine if a rope of a particular length can be split into the required lengths.
I was initially thinking of doing some sort of binary search in the search space [max length of rope required, ___], but I wasn't sure of what the upper bound should be. Also, I realised this wouldn't necessarily work since longer ropes does not necessarily guarantee that the rope can be split into the required lengths.
Right now, the only solution I have is a linear search, but this seems to be too slow and I'm not exactly sure the conditions that would result in there being no valid ropes.
Any guidance is greatly appreciated!