Given a set of numbers, I want to check if it is possible to produce the full set of numbers by repeatedly splitting one single starting number into its halves and doing the same for each of the halves' halves and so on, and if possible find out what is the smallest possible number to start with that can achieve this.
If a number
nto be split is odd, it will split intofloor(n/2)andceiling(n/2)respectively.
e.g. Given set
[1, 4, 5], the smallest possible number is18, as it splits into9and9, which then splits into4, 5and4, 5respectively. We keep one pair of4, 5, and split the other pair into2, 2, 2, 3, and finally1, 1, 1, 1, 1, 1, 1, 2. Thus,1, 4, 5are all obtained.
On the other hand, for set
[1, 2, 3, 4, 5, 6], it is impossible to obtain these numbers with any starting number.
How can I efficiently solve this problem? I'm aware that it seems to be some sort of integer partioning problem, but I can't think of any smart way to do this other than straight up brute forcing every number.
I gave this problem a shot and got a first idea which I want to share with you! EDIT: its wrong for case [1,10]!
Seem to be enough steps. Then i produced this code as it reduces the set and cannot find a single number until it diverges:
Now you only need to stop the validator with detected error. My Idea: If length stays same, the distances between some neighbours must fall or we stop when they rise.
Also you can calculate how many numbers are needed to produce the set and multiply the last number accordingly with some *2^n, the first example also gave (2^1)*9! //Edit this was bad, we need to ADD some +/- n to the final number.