import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{

		// Main array to be accessed from innermost to outermost element
		int[] arr = new int[]{1,2,3,4,5};
		int[] map = creatIndexMap(arr.length);
        printOut(arr, map);
        
        int[] arr2 = new int[]{1,2,3,4,5,6};
		int[] map2 = creatIndexMap(arr2.length);
        printOut(arr2, map2);
	}
	
	public static int[] creatIndexMap(int length) {
		
		// Create a deque so elements can be removed from both ends
		Deque<Integer> tmp = new LinkedList<Integer>();
		for(int i = 0; i < length; i++) {
			tmp.add(i);
		}
		
		// In alternation remove the last and first entries of tmp
		// and add them in reverse order to map array
		int[] map = new int[length];
		int index = length-1;
		while (!tmp.isEmpty()) {
			// Remove last element
			map[index--] = (int) tmp.removeLast();
			
			// Remove first element
			if(!tmp.isEmpty()) {
				map[index--] = (int) tmp.removeFirst();
			}
		}
		return map;
	}
	
	// Dispay results
	public static void printOut(int[] arr, int[] map) {
		System.out.println("Original array:");
		for(int i = 0; i < arr.length; i++) {
			System.out.print( arr[i] + " ");
		}
		
		System.out.println();
		System.out.println("Index-mapped array:");
		for(int i = 0; i < arr.length; i++) {
			System.out.print( arr[map[i]] + " ");
		}
		System.out.println();
	}
}