Given an unsorted array arr of size N, rearrange the elements of array such that number at the odd index is greater than the number at the previous even index. The task is to complete the function formatArray() which will return formatted array.
NOTE:
If the returned array formed is according to rules, 1 will be printed else 0 will be printed.
Example 1:
Input:
n = 5
arr[] = {5, 4, 3, 2, 1}
Output:
1
Explanation:
The given array after modification
will be as such: 4 5 2 3 1.
Example 2:
Input:
n = 4
arr[] = {4, 3, 2, 1}
Output:
1
User task:
Since this is a functional problem you don't have to worry about the input, you just have to complete the function formatArray(), which accepts array arr and its size  
Constraints:
1 <= T <= 100
1 <= N <= 100
1 <= arr[i] <= 100

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: def formatArray(a,n):

    # add code here

Solutions:
def formatArray(a, n):
	for i in range(0, n - 1, 2):
		if a[i + 1] < a[i]:
			(a[i + 1], a[i]) = (a[i], a[i + 1])
	return a


def formatArray(a, n):
	i = 1
	while i < n:
		if a[i] < a[i - 1]:
			(a[i], a[i - 1]) = (a[i - 1], a[i])
		i += 2
	return a


def formatArray(a, n):
	t = sorted(a, reverse=True)
	for i in range(0, len(t) - 1, 2):
		if i >= len(t) and i + 1 >= len(t):
			break
		else:
			a[i + 1] = t[i]
			a[i] = t[i + 1]
	return a


def formatArray(arr, n):
	for i in range(1, n, 2):
		if arr[i - 1] > arr[i]:
			(arr[i - 1], arr[i]) = (arr[i], arr[i - 1])
	return arr


def formatArray(a, n):
	a.sort()
	for i in range(0, n - 1, 2):
		j = i + 1
		if i % 2 != 0 and a[i] > arr[j]:
			(arr[i], arr[j]) = (arr[j], arr[i])
	return a


def formatArray(a, n):
	a.sort()
	return a


def formatArray(a, n):
	for i in range(1, n, 2):
		if a[i] >= a[i - 1]:
			continue
		else:
			(a[i], a[i - 1]) = (a[i - 1], a[i])
	return a


def formatArray(a, n):
	a.sort()
	for i in range(0, n):
		i += 1
		return a


def formatArray(a, n):
	for i in range(0, n, 2):
		a.sort()
		return a


def formatArray(a, n):
	a.sort()
	res = [0] * n
	l = 0
	r = n - 1
	for i in range(n):
		if i % 2 == 0:
			res[i] = a[l]
			l += 1
		else:
			res[i] = a[r]
			r -= 1
	return res


def formatArray(a, n):
	a[:] = sorted(a)
	return a


def formatArray(a, n):
	for i in range(0, len(a) - 1, 2):
		if a[i] < a[i + 1]:
			pass
		else:
			(a[i], a[i + 1]) = (a[i + 1], a[i])
	return a


def formatArray(a, n):
	for i in range(1, n):
		if a[i] > a[i - 1]:
			pass
		else:
			t = a[i - 1]
			a[i] = a[i - 1]
			a[i - 1] = t
	return a


def formatArray(a, n):
	a.sort(reverse=True)
	if n % 2 != 0:
		i = 0
		while i < n - 1:
			d = a[i]
			a[i] = a[i + 1]
			a[i + 1] = d
			i = i + 2
	else:
		i = 0
		while i < n:
			d = a[i]
			a[i] = a[i + 1]
			a[i + 1] = d
			i = i + 2
	return a


def formatArray(a, n):
	a.sort(reverse=True)
	if n % 2 != 0:
		i = 0
		f = 0
		sum = 0
		temp = 1
		g = 0
		while i < n - 1:
			d = a[i]
			a[i] = a[i + 1]
			a[i + 1] = d
			i = i + 2
	else:
		i = 0
		f = 0
		sum = 0
		temp = 1
		g = 0
		while i < n:
			d = a[i]
			a[i] = a[i + 1]
			a[i + 1] = d
			i = i + 2
	return a


def mergesort(arr):
	if len(arr) > 1:
		mid = len(arr) // 2
		L = arr[:mid]
		R = arr[mid:]
		mergesort(L)
		mergesort(R)
		i = j = k = 0
		while i < len(L) and j < len(R):
			if L[i] < R[j]:
				arr[k] = L[i]
				i += 1
			else:
				arr[k] = R[j]
				j += 1
			k += 1
		while i < len(L):
			arr[k] = L[i]
			k += 1
			i += 1
		while j < len(R):
			arr[k] = R[j]
			k += 1
			j += 1

def formatArray(a, n):
	temp = a
	for i in range(n):
		if i % 2 == 0 and i < n - 1 and (temp[i] > temp[i + 1]):
			(temp[i], temp[i + 1]) = (temp[i + 1], temp[i])
	return temp


def formatArray(a, n):
	for (index, value) in enumerate(a):
		if index == 0 or index % 2 == 0:
			try:
				next_ = a[index + 1]
				if next_ < value:
					a[index + 1] = value
					a[index] = next_
			except IndexError:
				return a
		else:
			try:
				next_ = a[index + 1]
				if next_ > value:
					a[index + 1] = value
					a[index] = next_
			except IndexError:
				return a


def formatArray(a, n):
	for i in range(n):
		if i % 2 == 0:
			if i + 1 < n and a[i] > a[i + 1]:
				b = a[i]
				c = a[i + 1]
				a[i] = c
				a[i + 1] = b
	return a


def formatArray(a, n):
	a.sort()
	x = 0
	y = 0
	for j in range(n):
		if j % 2 == 0:
			a.append(n // 2 - x)
			x -= 1
		else:
			a.append(n // 2 + y)
			y += 1
	for j in range(n):
		a.remove(a[0])
	return a


def formatArray(a, n):
	for i in range(0, n, 2):
		if i + 1 < n:
			if a[i + 1] < a[i]:
				temp = a[i]
				a[i] = a[i + 1]
				a[i + 1] = temp
		else:
			break
	return a


def formatArray(a, n):
	copy = sorted(a)
	for x in range(n):
		if x % 2 == 0:
			a[x] = copy[0]
			del copy[0]
		else:
			a[x] = copy.pop()
	return a


def formatArray(a, n):
	a = sorted(a)[::-1]
	l1 = [i for i in a[::2]]
	l2 = [i for i in a[1::2]]
	l = []
	if len(a) % 2 == 0:
		for i in range(len(l1)):
			if l1[i] >= l2[i]:
				l.extend([l2[i], l1[i]])
	else:
		for i in range(len(l2)):
			if l1[i] >= l2[i]:
				l.extend([l2[i], l1[i]])
		l.append(l1[-1])
	return l


def formatArray(a, n):
	if len(a) == n:
		a.sort()
		return a


def formatArray(a, n):
	for i in range(0, n, 2):
		if i == n - 1:
			break
		elif a[i] > a[i + 1]:
			(a[i], a[i + 1]) = (a[i + 1], a[i])
		else:
			continue
	return a


def formatArray(a, n):
	for i in range(1, n):
		if i & 1 and a[i - 1] > a[i]:
			(a[i - 1], a[i]) = (a[i], a[i - 1])
	return a