Given a list of integers, rearrange the list such that it consists of alternating minimum-maximum elements using only list operations. The first element of the list should be the minimum of all elements and the second element should be a maximum of all elements present in the list. Similarly, the third element will be the next minimum element and the fourth element is the next maximum element, and so on. Use of extra space is not permitted. Store the answer in the answer[] array. You don't need to print that.
Note : All the elements in the array are unique.
Example 1:
Input
5
4 5 1 2 3
Output
1 5 2 4 3
Explanation:
In the first example minimum element is 1,
maximum element is 5, second minimum is
2 and so on, thus the rearranged array is
[1, 5, 2, 4, 3]
 
Example 2:
Input
4
1 2 3 4
Output
1 4 2 3 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array A[] and its size N as inputs and stores the final modified list in the answer[].
Expected Time Complexity: O(N.log(N))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ A[i] ≤ 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3

class Solution:    
    def Rearrange(self, a, n, answer):
        #Code Here
    

Solutions:
class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		s = 0
		e = n - 1
		for i in range(n):
			if i % 2 == 0:
				answer[i] = a[s]
				s += 1
			else:
				answer[i] = a[e]
				e -= 1
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		answer.clear()
		m = 0
		l = 0
		h = n - 1
		a.sort()
		while l <= h:
			if l == h:
				answer.append(a[l])
				m = 1
				break
			answer.append(a[l])
			l = l + 1
			answer.append(a[h])
			h = h - 1
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		x = a[::-1]
		answer.clear()
		if n % 2 != 0:
			for i in range(n // 2):
				answer.append(a[i])
				answer.append(x[i])
				continue
			answer.append(a[n // 2])
		else:
			for j in range(n // 2):
				answer.append(a[j])
				answer.append(x[j])
				continue
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		d = sorted(a)
		t = sorted(a, reverse=True)
		k = 0
		for i in range(0, n - 1, 2):
			if i >= n and i + 1 >= n:
				break
			answer[i] = d[k]
			answer[i + 1] = t[k]
			k = k + 1
		answer[-1] = d[k]


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		(k, p) = (0, n - 1)
		for i in range(n):
			if i % 2 == 0:
				answer[i] = a[k]
				k += 1
			else:
				answer[i] = a[p]
				p -= 1
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		k = []
		p = []
		p[:] = a[:]
		p.sort()
		p.reverse()
		a.sort()
		i = 0
		while len(k) < n:
			k.append(a[i])
			if len(k) == n:
				break
			k.append(p[i])
			i = i + 1
		answer[:] = k[:]
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		l = []
		i = 0
		while len(l) < len(a):
			l.append(a[i])
			l.append(a[len(a) - 1 - i])
			i += 1
		for i in range(len(answer)):
			answer[i] = l[i]
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		start = 0
		end = n - 1
		for i in range(n):
			if i % 2 == 0:
				answer[i] = a[start]
				start += 1
			else:
				answer[i] = a[end]
				end -= 1
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		a = sorted(a)
		right = n - 1
		left = 0
		i = 0
		while left <= right:
			if i % 2 == 0:
				answer[i] = a[left]
				left += 1
			else:
				answer[i] = a[right]
				right -= 1
			i += 1


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		l = 0
		r = n - 1
		for i in range(n):
			if i % 2 == 0:
				answer[i] = a[l]
				l += 1
			else:
				answer[i] = a[r]
				r -= 1


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		i = 0
		j = n - 1
		k = 0
		while k < n:
			answer[k] = a[i]
			k += 1
			if k < n:
				answer[k] = a[j]
			k += 1
			i += 1
			j -= 1


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		min = 0
		max = n - 1
		for i in range(n):
			if i % 2 == 0:
				answer[i] = a[min]
				min += 1
			else:
				answer[i] = a[max]
				max -= 1
		return answer


class Solution:

	def Rearrange(self, a, n, ar):
		a.sort()
		mini = 0
		maxi = n - 1
		for i in range(n):
			if i % 2 == 0:
				ar[i] = a[mini]
				mini += 1
			else:
				ar[i] = a[maxi]
				maxi -= 1
		return ar


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		(mini, maxi) = (0, n - 1)
		for ind in range(n):
			if ind % 2 == 0:
				answer[ind] = a[mini]
				mini += 1
			else:
				answer[ind] = a[maxi]
				maxi -= 1
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		asc = sorted(a)
		desc = sorted(a, reverse=True)
		if n % 2 == 0:
			answer[0:n:2] = asc[0:n // 2]
			answer[1:n:2] = desc[0:n // 2]
		else:
			answer[0:n:2] = asc[0:n // 2 + 1]
			answer[1:n:2] = desc[0:n // 2]


class Solution:

	def Rearrange(self, a, n, answer):
		asc = sorted(a)
		desc = sorted(a, reverse=True)
		j = 0
		k = 0
		for i in range(n):
			if i % 2 == 0:
				answer[i] = asc[j]
				j += 1
			if i % 2 != 0:
				answer[i] = desc[k]
				k += 1


class Solution:

	def Rearrange(self, a, n, answer):
		answer.clear()
		if len(a) < 3:
			a.sort()
			for i in a:
				answer.append(i)
		else:
			a.sort()
			ans1 = a[:n // 2 + 1]
			ans2 = a[n // 2 + 1:]
			ans2.sort(reverse=1)
			min = len(ans2)
			if len(ans1) < len(ans2):
				min = len(ans1)
			rem = 0
			for i in range(min):
				answer.append(ans1[i])
				answer.append(ans2[i])
				rem = i
			for i in range(rem + 1, len(ans1)):
				answer.append(ans1[i])
			for i in range(rem + 1, len(ans2)):
				answer.append(ans2[i])


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		b = 0
		c = n - 1
		for i in range(len(a)):
			if i % 2 == 0:
				answer[i] = a[b]
				b += 1
			else:
				answer[i] = a[c]
				c -= 1
		return answer


class Solution:

	def partition(self, arr, l, r):
		pivot = arr[r]
		i = l - 1
		for j in range(l, r):
			if arr[j] < pivot:
				i += 1
				(arr[i], arr[j]) = (arr[j], arr[i])
		(arr[i + 1], arr[r]) = (arr[r], arr[i + 1])
		return i + 1

	def quicksort(self, arr, l, r):
		mid = (l + r) // 2
		if l < r:
			p = self.partition(arr, l, r)
			self.quicksort(arr, l, p - 1)
			self.quicksort(arr, p + 1, r)

	def Rearrange(self, a, n, answer):
		a.sort()
		j = 0
		l = []
		while len(l) < len(a):
			l.append(a[j])
			l.append(a[len(a) - j - 1])
			j += 1
		for i in range(len(answer)):
			answer[i] = l[i]
		return answer


class Solution:

	def partition(self, arr, l, r):
		pivot = arr[r]
		i = l - 1
		for j in range(l, r):
			if arr[j] < pivot:
				i += 1
				(arr[i], arr[j]) = (arr[j], arr[i])
		(arr[i + 1], arr[r]) = (arr[r], arr[i + 1])
		return i + 1

	def quicksort(self, arr, l, r):
		mid = (l + r) // 2
		if l < r:
			p = self.partition(arr, l, r)
			self.quicksort(arr, l, p - 1)
			self.quicksort(arr, p + 1, r)

	def Rearrange(self, a, n, answer):
		a.sort()
		j = 0
		maxi = len(a) - 1
		mini = 0
		for i in range(len(a)):
			if i % 2 == 0:
				answer[i] = a[mini]
				mini += 1
			else:
				answer[i] = a[maxi]
				maxi -= 1
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		i = 0
		j = n - 1
		while i < j:
			answer.append(a[i])
			answer.append(a[j])
			i = i + 1
			j = j - 1
		if i == j:
			answer.append(a[i])
		del answer[0:n]
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		array = []
		for i in range(n // 2 + 1):
			array.append(a[i])
			array.append(a[-(i + 1)])
		answer[:] = array[:n]
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		a = sorted(a)
		mini = 0
		maxx = len(a) - 1
		for i in range(n):
			if i % 2 == 0:
				answer[i] = a[mini]
				mini += 1
			else:
				answer[i] = a[maxx]
				maxx -= 1
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		j = 0
		for i in range(0, n, 2):
			answer[i] = a[j]
			j += 1
		j = -1
		for i in range(1, n, 2):
			answer[i] = a[j]
			j -= 1


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		x = a[::-1]
		l = []
		i = 0
		while len(l) <= n:
			l.append(a[i])
			l.append(x[i])
			i += 1
		answer.clear()
		if n % 2 == 0:
			return answer.extend(l[0:n])
		else:
			return answer.extend(l[0:n])


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		r = []
		f = 0
		s = 0
		e = n - 1
		for j in range(n):
			if f == 0:
				r.append(a[s])
				s += 1
				f = 1
			else:
				r.append(a[e])
				e -= 1
				f = 0
		answer[:] = r[:]


from itertools import cycle

class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		b = a[::-1]
		iters = [iter(a), iter(b)]
		d = list((iter.__next__() for iter in cycle(iters)))
		for i in range(0, n):
			answer[i] = d[i]
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		l = 0
		lp = n - 1
		flag = True
		i = 0
		while i < n:
			if flag:
				answer[i] = a[l]
				l += 1
				i += 1
			else:
				answer[i] = a[lp]
				lp -= 1
				i += 1
			flag = bool(1 - flag)
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		r = 0
		l = n - 1
		a.sort()
		i = 0
		while r < l:
			answer[i] = a[r]
			r += 1
			i += 1
			answer[i] = a[l]
			l -= 1
			i += 1
		if n % 2 == 1:
			answer[-1] = a[n // 2]


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		minn = 0
		maxx = n - 1
		for i in range(n):
			if i % 2 == 0:
				answer[i] = a[minn]
				minn += 1
			else:
				answer[i] = a[maxx]
				maxx -= 1
		return answer


class Solution:

	def Rearrange(self, a, n, ans):
		right = n - 1
		left = 0
		a.sort()
		i = 0
		while right > left:
			ans[i] = a[left]
			left += 1
			i += 1
			ans[i] = a[right]
			right -= 1
			i += 1
		if n % 2 == 1:
			ans[-1] = a[n // 2]


class Solution:

	def Rearrange(self, a, n, answer):
		a = sorted(a)
		for i in range(n // 2):
			answer.append(a[i])
			answer.append(a[n - i - 1])
		del answer[:n]
		if n % 2 != 0:
			answer.append(a[n // 2])


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		(i, j) = (0, len(a) - 1)
		for k in range(n):
			if k % 2 == 0:
				answer[k] = a[i]
				i += 1
			else:
				answer[k] = a[j]
				j -= 1


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		l = 0
		r = n
		ch = 1
		j = 0
		k = n - 1
		for i in range(n):
			if ch == 1:
				answer[i] = a[j]
				j += 1
				ch = 0
			elif ch == 0:
				answer[i] = a[k]
				k -= 1
				ch = 1
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		m = n // 2
		b = a[-m:]
		b.sort(reverse=True)
		j = 0
		k = 0
		for i in range(n):
			if i % 2 == 0:
				answer[i] = a[j]
				j += 1
			else:
				answer[i] = b[k]
				k += 1


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		start = 0
		end = n - 1
		l = []
		while start <= end:
			l.append(a[start])
			l.append(a[end])
			start += 1
			end -= 1
		if n % 2 != 0:
			m = l[0:n - 1]
		else:
			m1 = l
		for i in range(n):
			answer[i] = l[i]
		return answer[i]


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		i = 0
		j = n - 1
		m = 0
		while m < n:
			if m % 2 == 0:
				answer[m] = a[i]
				i += 1
			else:
				answer[m] = a[j]
				j -= 1
			m += 1


class Solution:

	def Rearrange(self, a, n, answer):
		a = sorted(a)
		i = 0
		j = n - 1
		l = 0
		while i < j:
			answer[l] = a[i]
			l += 1
			answer[l] = a[j]
			l += 1
			i += 1
			j -= 1
		if n % 2 != 0:
			answer[l] = a[i]


class Solution:

	def Rearrange(self, a, n, ans):
		a.sort()
		(i, j) = (0, n - 1)
		k = 0
		while i < j:
			(ans[k], ans[k + 1]) = (a[i], a[j])
			i += 1
			j -= 1
			k += 2
		if i == j:
			ans[k] = a[i]


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		big = n - 1
		small = 0
		for i in range(n):
			if i % 2 == 0:
				answer[i] = a[small]
				small += 1
			else:
				answer[i] = a[big]
				big -= 1
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		for i in range(1, n, 2):
			value = a.pop()
			answer[i] = value
		x = 0
		for j in range(0, len(answer), 2):
			answer[j] = a[x]
			x = x + 1


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		mi = 0
		ma = n - 1
		for i in range(n):
			if i % 2 == 0:
				answer[i] = a[mi]
				mi += 1
			else:
				answer[i] = a[ma]
				ma -= 1
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		b = []
		a.sort()
		if n == 1:
			answer[0] = a[0]
		if n == 2:
			answer[0] = a[0]
			answer[1] = a[1]
		if n % 2 == 0 and n > 2:
			for i in range(n // 2):
				b.append(a[i])
				b.append(a[n - 1 - i])
			b.append(a[n // 2 + 1])
			for i in range(n):
				answer[i] = b[i]
		elif n % 2 != 0 and n > 2:
			for i in range(n // 2):
				b.append(a[i])
				b.append(a[n - 1 - i])
			b.append(a[n // 2])
			for i in range(n):
				answer[i] = b[i]


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		(ptr1, ptr2, flag, i) = (0, n - 1, False, 0)
		while ptr1 <= ptr2:
			if flag:
				answer[i] = a[ptr2]
				(flag, ptr2) = (not flag, ptr2 - 1)
			else:
				answer[i] = a[ptr1]
				(flag, ptr1) = (not flag, ptr1 + 1)
			i += 1
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		minidx = 0
		maxidx = -1
		for i in range(0, n, 2):
			answer[i] = a[minidx]
			minidx += 1
		for i in range(1, n, 2):
			answer[i] = a[maxidx]
			maxidx -= 1
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		(i, left, right) = (0, 0, n - 1)
		while left < right:
			answer[i] = a[left]
			answer[i + 1] = a[right]
			left += 1
			right -= 1
			i += 2
		if n % 2:
			answer[-1] = a[n // 2]
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		a = sorted(a)
		i = 0
		j = len(a) - 1
		k = 0
		while i < j:
			answer[k] = a[i]
			i = i + 1
			k = k + 1
			answer[k] = a[j]
			j = j - 1
			k = k + 1
		if i == j:
			answer[k] = a[i]
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		low = 0
		high = int(n - 1)
		i = 0
		a.sort()
		while low < high:
			answer[i] = a[low]
			i = i + 1
			answer[i] = a[high]
			i = i + 1
			low = low + 1
			high = high - 1
		if low == high:
			answer[i] = a[low]


class Solution:

	def Rearrange(self, nums, n, res):
		nums = sorted(nums)
		i = 0
		j = len(nums) - 1
		k = 0
		while i < j:
			res[k] = nums[i]
			i = i + 1
			k = k + 1
			res[k] = nums[j]
			j = j - 1
			k = k + 1
		if i == j:
			res[k] = nums[i]
		return res


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		arr = []
		for i in range(n):
			arr.append(a[i])
			arr.append(a[-(i + 1)])
		answer[:] = arr[:n]
		return answer


class Solution:

	def Rearrange(self, a, n, answer):
		a.sort()
		answer.clear()
		for i in range(n // 2):
			answer.append(a[i])
			answer.append(a[n - i - 1])
		if n % 2 != 0:
			answer.append(a[n // 2])
		return answer