Given a sorted array arr[] of distinct integers. Sort the array into a wave-like array(In Place).
In other words, arrange the elements into a sequence such that arr[1] >= arr[2] <= arr[3] >= arr[4] <= arr[5].....
If there are multiple solutions, find the lexicographically smallest one.
Note:The given array is sorted in ascending order, and you don't need to return anything to make changes in the original array itself.
Example 1:
Input:
n = 5
arr[] = {1,2,3,4,5}
Output: 2 1 4 3 5
Explanation: Array elements after 
sorting it in wave form are 
2 1 4 3 5.
Example 2:
Input:
n = 6
arr[] = {2,4,7,8,9,10}
Output: 4 2 8 7 10 9
Explanation: Array elements after 
sorting it in wave form are 
4 2 8 7 10 9.
Your Task:
The task is to complete the function convertToWave(), which converts the given array to a wave array.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{6}
0 ≤ arr[i] ≤10^{7}

Source: geeksforgeeks | Difficulty: EASY
Tags: Sorting

Data structures

Starter code: from typing import List





class Solution:

    def convertToWave(self, n : int, a : List[int]) -> None:

        # code here

        



Solutions:
from typing import List

class Solution:

	def convertToWave(self, n: int, a: List[int]) -> None:
		(l, r) = (0, 1)
		while r < n:
			a[l] = a[l] ^ a[r]
			a[r] = a[l] ^ a[r]
			a[l] = a[l] ^ a[r]
			l += 2
			r = l + 1
		return a


from typing import List

class Solution:

	def convertToWave(self, n: int, a: List[int]) -> None:
		i = 0
		while i < n - 1:
			t = a[i]
			a[i] = a[i + 1]
			a[i + 1] = t
			i = i + 2


from typing import List

class Solution:

	def convertToWave(self, n: int, a: List[int]) -> None:
		for i in range(0, n - 1, 2):
			(a[i], a[i + 1]) = (a[i + 1], a[i])


from typing import List

class Solution:

	def convertToWave(self, n: int, a: List[int]) -> None:
		i = 1
		while i < n:
			temp = a[i - 1]
			a[i - 1] = a[i]
			a[i] = temp
			i += 2
		return a


from typing import List

class Solution:

	def convertToWave(self, n: int, a: List[int]) -> None:
		if len(a) != 2:
			for i in range(0, len(a) - 1, 2):
				if a[i] < a[i + 1]:
					t = a[i]
					a[i] = a[i + 1]
					a[i + 1] = t
		elif a[1] > a[0]:
			t = a[0]
			a[0] = a[1]
			a[1] = t


from typing import List

class Solution:

	def convertToWave(self, n: int, a: List[int]) -> None:
		i = 0
		for j in range(1, len(a) - i, 2):
			if a[i] < a[j]:
				(a[i], a[j]) = (a[j], a[i])
				i = i + 2
			else:
				i = i + 2
		return a


from typing import List

class Solution:

	def convertToWave(self, n: int, a: List[int]) -> None:
		for index in range(0, n, 2):
			if index < 0 and a[index] < a[index - 1]:
				(a[index], a[index - 1]) = (a[index - 1], a[index])
			if index < n - 1 and a[index] < a[index + 1]:
				(a[index], a[index + 1]) = (a[index + 1], a[index])
		return a


from typing import List

class Solution:

	def convertToWave(self, n: int, a: List[int]):
		for i in range(n):
			if i % 2 == 1:
				(a[i - 1], a[i]) = (a[i], a[i - 1])
		return a


from typing import List

class Solution:

	def convertToWave(self, n: int, arr: List[int]) -> None:
		for i in range(0, n - 1, 2):
			if i == 0 and arr[i] <= arr[i + 1]:
				(arr[i], arr[i + 1]) = (arr[i + 1], arr[i])
			elif i == n - 1 and arr[i - 1] >= arr[i]:
				(arr[i - 1], arr[i]) = (arr[i], arr[n - 1])
			else:
				if arr[i] < arr[i + 1]:
					(arr[i], arr[i + 1]) = (arr[i + 1], arr[i])
				if arr[i - 1] > arr[i]:
					(arr[i - 1], arr[i]) = (arr[i], arr[n - 1])


from typing import List

class Solution:

	def convertToWave(self, n: int, a: List[int]) -> None:
		if n % 2 == 0:
			for i in range(1, n, 2):
				temp = a[i - 1]
				temp2 = a[i]
				a[i - 1] = temp2
				a[i] = temp
		else:
			for i in range(1, n - 1, 2):
				temp = a[i - 1]
				temp2 = a[i]
				a[i - 1] = temp2
				a[i] = temp


from typing import List

class Solution:

	def convertToWave(self, n: int, arr: List[int]) -> None:
		arr.sort()
		for i in range(0, n - 1, 2):
			(arr[i], arr[i + 1]) = (arr[i + 1], arr[i])
		return arr


from typing import List

class Solution:

	def convertToWave(self, n: int, arr: List[int]) -> None:
		end = n - 1 if n % 2 == 0 else n - 2
		for i in range(0, end + 1, 2):
			temp = arr[i]
			arr[i] = arr[i + 1]
			arr[i + 1] = temp


from typing import List

class Solution:

	def convertToWave(self, n, array):
		for i in range(0, n, 2):
			if i > 0 and array[i] < array[i - 1]:
				(array[i], array[i - 1]) = (array[i - 1], array[i])
			if i < n - 1 and array[i] < array[i + 1]:
				(array[i], array[i + 1]) = (array[i + 1], array[i])
		return None


from typing import List

class Solution:

	def convertToWave(self, n: int, a: List[int]) -> None:
		track = 0
		if n % 2 == 0:
			track = n
		else:
			track = n - 1
		count = 0
		while count < track:
			(a[count], a[count + 1]) = (a[count + 1], a[count])
			count = count + 2
		return a


from typing import List

class Solution:

	def convertToWave(self, n: int, a: List[int]) -> None:
		for idx in range(0, n, 2):
			if idx == n - 1:
				continue
			temp = a[idx]
			a[idx] = a[idx + 1]
			a[idx + 1] = temp


from typing import List

class Solution:

	def convertToWave(self, n: int, a: List[int]) -> None:
		i = 0
		while i < n - 1:
			j = i + 1
			val = a[i]
			a[i] = a[j]
			a[j] = val
			i += 2
		return a


from typing import List

class Solution:

	def convertToWave(self, n: int, a: List[int]) -> None:
		l = []
		l[:] = a[:]
		i = 0
		j = 1
		while j <= n:
			(l[i], l[j]) = (l[j], l[i])
			i = i + 2
			if i == len(l) - 1:
				break
			j += 2
		a[:] = l[:]