This problem can be solved using the stack and the original array. I will not code the solution for you, but I will indicate how to solve it.
- push the elements of the array onto the stack, following the rules that we discuss below.
- right after that, return the stack back to the array, starting at index 0
- repeated until the final condition is met.
Rule for filling the stack:
This is a bit abstract, so consider your example:
iter = 1 ->1234 <-5678 Arrows indicate the direction of iteration
start from the end and fill the stack; inter odd so start with the first odd half occurring
5 6 7 8 4 <-notice that the order of pushing the halfs on the stack is shown by the arrows 3 2 1
put the stop back: 5 6 7 8 4 3 2 1
Keep halving:
iter = 2 <-56 ->78 <-43 ->21 ; odd halves 56 , 43 ; even half 78 , 21
start from the end and fill the stack; inter even starts with the first even halves
5 6 4 3 8 <-even halfs end, odd halfs start 7 1 2
Put the stack back: 5 6 4 3 8 7 1 2
Separate the segments again, since in each new half of the arrow only one element will be used to highlight the rule:
iter = 3 ->5 <-6 ->4 <-3 ->8 <-7 ->1 <-2
iter is odd, so fill the stack with odd halves first
6 3 7 2 5 4 8 1
Put the stack back and you 63725481 done: 63725481
Hope this makes sense; happy coding :)