@@ -0,0 +1,1453 @@
+import unittest%0A%22%22%22%0AGiven a sorted array of positive integers, rearrange the array such that first element%0Ais max, second element is min, third element is second max, fourth element is second min and so on.%0AInput: 1 2 3 4 5 6 7%0AOutput: 7 1 6 2 5 3 4%0AInput: 1 2 3 4 5 6%0AOutput: 6 1 5 2 4 3%0A%22%22%22%0A%0A%22%22%22%0AApproach:%0A1. Create an index array such that index%5Bi%5D contains the correct output position for arr%5Bi%5D.%0A2. Swap elements by following the indices as specified by index array.%0A%22%22%22%0A%0A%0Adef rearrange_max_min(list_of_numbers):%0A%0A    end = len(list_of_numbers)%0A    max_i = end-1%0A    min_i = 0%0A    index = %5B%5D%0A%0A    while len(index) != end:%0A        index.append(max_i)%0A        if max_i != min_i:%0A            index.append(min_i)%0A        max_i -= 1%0A        min_i += 1%0A%0A    for i in range(end):%0A        if list_of_numbers%5Bi%5D %3C 0:%0A            continue%0A        j = i%0A        k = index%5Bi%5D%0A        while k != i:%0A            list_of_numbers%5Bj%5D, list_of_numbers%5Bk%5D = list_of_numbers%5Bk%5D, list_of_numbers%5Bj%5D%0A            list_of_numbers%5Bj%5D = -list_of_numbers%5Bj%5D%0A            k = index%5Bindex%5Bj%5D%5D%0A            j = index%5Bj%5D%0A        list_of_numbers%5Bj%5D = -list_of_numbers%5Bj%5D%0A%0A    list_of_numbers = %5B-x for x in list_of_numbers%5D%0A    return list_of_numbers%0A%0A%0Aclass TestRearrange(unittest.TestCase):%0A%0A    def test_rearrange(self):%0A        list_of_numbers = %5B1, 2, 3, 4, 5, 6, 7%5D%0A        result = rearrange_max_min(list_of_numbers)%0A        self.assertEqual(result, %5B7, 1, 6, 2, 5, 3, 4%5D)%0A%0A%0A

Rearrange min max