Cut and Stack the Array

Viewed 137

I have an array of int as input. The array will always be a length of 2^n. I want to cut the array in half and stack it. Repeat cutting and stacking until there is only one stack. For example:

int[] array = {1,2,3,4,5,6,7,8}

if we cut the array in half we and stack them it would be:

{1,2,| 3,4}
{5,6,| 7,8}

cut it and stack again:

{1,|2}
{5,|6}
{3,|4}
{7,|8}

again:

{1}
{5}
{3}
{7}
{2}
{6}
{4}
{8}

the desired output would be an array of int from the top to the end of the stack

int[] output =  {1,5,3,7,2,6,4,8}

I have tried to construct the output array by looping the input array in a particular way. Note that when I reach the array I just start from the head again. I start at the array[i] where i = 0. I get the first number this way. then I increment i by n where n is the log(array.legnth) (base 2). This is how I get the second number. For the third number, we increment i by (n + n/2). For the fourth number, we increment i by n again. I am wondering is there a pattern? Or what would be your approach to solve this problem? I am looking for a solution in java or python.


Edit/Update:

I tried a new approach using queue. I basically keep cutting array in half and push both half of array into queue until arrays in queue all have length of 2 (or the stack is at height n). but my result is not correct. I think it has to do with the order I add the half arrays back into the queue.

import java.util.*;

public class StackArray{
    public static void main(String[] args){
        int[] array = {1,2,3,4,5,6,7,8};
        int[] answer = stackArray(array);
        for(int n : answer){
            System.out.println(n);
        }   
    }   

    public static int[] stackArray(int[] array){
        int height = (int) (Math.log(array.length)/Math.log(2)) + 1;
        Queue<int[]> queue = new LinkedList<int[]>();
        ArrayList<Integer> list = new ArrayList<>();
        queue.add(array);
        while(queue.size() < height){
            int currentLength = queue.size();
            int i = 0;
            while(i < currentLength){
                int[] temp = queue.poll();
                int[] array1 = new int[temp.length/2];
                int[] array2 = new int[temp.length/2];
                System.arraycopy(temp, 0, array1, 0, array1.length);
                System.arraycopy(temp, array1.length, array2, 0, array2.length);
                queue.add(array1); //I think the problem is here
                queue.add(array2);
                i++;
            }

        }


        int y = 0;
        while(y < 2){

            for(int i = 0; i < queue.size(); i++){
                int[] curr = queue.poll();
                list.add(curr[y]);
                queue.add(curr);

            }
            y++;
        }

        int[] ret = new int[list.size()];
        for(int i = 0; i < list.size(); i++){
            ret[i] =list.get(i);
        }

        return ret;

    }
}

result:

1
3
5
7
2
4
6
8

how would I fix this?


update: I solved it and posted my own answer. but I am still curious as of how would other people solve this. Please stil feel free to answer.

2 Answers
Related