Given an array arr of n positive integers. The task is to swap every i^{th} element of the array with (i+2)^{th} element.
Note: When passing array to function, we basically pass the reference to the array. More details here. We'll learn more about pointers and references in upcoming module.
Example:
Input:
n = 5
arr[] = 1 2 3 4 5
Output:
3 4 5 2 1
Explanation:
Swapping 1 and 3, makes the array 3 2 1 4 5.
Now, swapping 2 and 4, makes the array 
3 4 1 2 5. Again,swapping 1 and 5, makes the 
array 3 4 5 2 1.
Your Task:
Your task is to complete the function swapElements(), which should swap each ith element with (i+2)^{th} element. No need to print or return the array.
 
Expected Time Complexity: O(n)
Expected Auxilary Space: O(1)
Constraints:
1 <= N <= 10^{6}
1 <= A_{i} <= 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3



class Solution:

	def swapElements(self, arr, n):

		#Code here

Solutions:
class Solution:

	def swapElements(self, arr, n):
		for i in range(n - 2):
			item1 = arr[i]
			item2 = arr[i + 2]
			arr[i + 2] = item1
			arr[i] = item2


class Solution:

	def swapElements(self, arr, n):
		for i in range(n - 2):
			(arr[i], arr[i + 2]) = (arr[i + 2], arr[i])
		return arr


class Solution:

	def swapElements(self, arr, n):
		for i in range(0, n - 2):
			temp = arr[i]
			arr[i] = arr[i + 2]
			arr[i + 2] = temp


class Solution:

	def swapElements(self, arr, n):
		for i in range(n):
			if i != n - 2:
				(arr[i], arr[i + 2]) = (arr[i + 2], arr[i])
			else:
				break


class Solution:

	def swapElements(self, arr, n):
		for i in range(0, n):
			if i + 2 < n:
				a = arr[i]
				b = arr[i + 2]
				arr[i] = b
				arr[i + 2] = a


class Solution:

	def swapElements(self, arr, n):
		i = 0
		while i + 2 < n:
			(arr[i], arr[i + 2]) = (arr[i + 2], arr[i])
			i += 1


class Solution:

	def swapElements(self, arr, n):
		for i in range(0, len(arr) - 2):
			t = arr[i]
			arr[i] = arr[i + 2]
			arr[i + 2] = t
		return arr


class Solution:

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


class Solution:

	def swapElements(self, arr, n):
		for index in range(n - 2):
			(arr[index], arr[index + 2]) = (arr[index + 2], arr[index])


class Solution:

	def swapElements(self, arr, n):
		for i in range(n - 2):
			firstElement = arr[i]
			secondElement = arr[i + 2]
			arr[i] = secondElement
			arr[i + 2] = firstElement


class Solution:

	def swapElements(self, arr, n):
		arrr = arr
		for a in range(n):
			if a < n - 2:
				aux = arrr[a]
				arrr[a] = arrr[a + 2]
				arrr[a + 2] = aux
		return arrr


class Solution:

	def swapElements(self, arr, n):
		if n > 2:
			i = 2
			while i < n:
				(arr[i - 2], arr[i]) = (arr[i], arr[i - 2])
				i += 2
			i = 3
			while i < n:
				(arr[i - 2], arr[i]) = (arr[i], arr[i - 2])
				i += 2


class Solution:

	def swapElements(self, arr, n):
		for i in range(1, n):
			if i + 1 != len(arr):
				temp = arr[i - 1]
				arr[i - 1] = arr[i + 1]
				arr[i + 1] = temp


class Solution:

	def swapElements(self, arr, n):
		if n % 2 == 0:
			a = arr[2:] + arr[0:2]
		else:
			a = arr[2:] + [arr[1]] + [arr[0]]
		for i in range(n):
			arr[i] = a[i]


class Solution:

	def swapElements(self, arr, n):
		i = 0
		j = i + 2
		while j < len(arr):
			(arr[i], arr[j]) = (arr[j], arr[i])
			i = i + 1
			j = i + 2
		return arr


class Solution:

	def swapElements(self, arr, n):
		ele = 0
		l = []
		for i in range(2):
			ele = arr.pop(0)
			l.append(ele)
		if n % 2 != 0:
			arr.append(l[1])
			arr.append(l[0])
		else:
			arr.append(l[0])
			arr.append(l[1])


class Solution:

	def swapElements(self, arr, n):
		y = 0
		while y < 2:
			x = arr[y]
			for i in range(y, n - 2, 2):
				f = i
				arr[i] = arr[i + 2]
			arr[f + 2] = x
			y += 1
		return arr


class Solution:

	def swapElements(self, arr, n):
		for i in range(len(arr) - 2):
			c = arr[i]
			arr[i] = arr[i + 2]
			arr[i + 2] = c


import copy

class Solution:

	def swapElements(self, arr, n):
		i = 0
		while i < n - 2:
			(arr[i], arr[i + 2]) = (arr[i + 2], arr[i])
			i = i + 1
		return arr