"""
Solution for Rearrange Array Alternately
https://practice.geeksforgeeks.org/problems/-rearrange-array-alternately/0/
"""
def alternate_array(arr):
    """
    Given a sorted array of positive integers. Your task is to rearrange  the array elements alternatively i.e first element should be max value, second should be min value, third should be second max, fourth should be second min and so on...

    Note: O(1) extra space is allowed. Also, try to modify the input array as required.

    Input:
    First line of input conatins number of test cases T. First line of test case contain an integer denoting the array size N and second line of test case contain N space separated integers denoting the array elements.

    Output:
    Output the modified array with alternated elements.

    Constraints:
    1 <=T<= 100
    1 <=N<= 107
    1 <=arr[i]<= 107

    Example:
    Input:
    2
    6
    1 2 3 4 5 6
    11
    10 20 30 40 50 60 70 80 90 100 110

    Output:
    6 1 5 2 4 3
    110 10 100 20 90 30 80 40 70 50 60

    Explanation:
    Testcase 1: Max element = 6, min = 1, second max = 5, second min = 2, and so on... Modified array is : 6 1 5 2 4 3.

    Args:
        arr(list[int]):

    Returns:
        list[int]:

    """
    # At any index i, record the original value and new value using % and // operator
    max_element = arr[-1] + 1
    min_index, max_index = 0, len(arr) - 1

    for i in range(len(arr)):
        if i % 2 == 0:
            arr[i] += (arr[max_index] % max_element) * max_element
            max_index -= 1
        else:
            arr[i] += (arr[min_index] % max_element) * max_element
            min_index += 1

    for i in range(len(arr)):
        arr[i] //= max_element

    print(' '.join(map(str, arr)))
    return 1
