# Swap Alternate Boundary Pairs

Given an array arr[] of N integers, the task is to swap the first and the last element then the third and the third last element then fifth and fifth last and so on. Print the final array after all the valid operations.

Input: arr[] = {1, 2, 3, 4, 5, 6}
Output: 6 2 4 3 5 1
Operation 1: Swap 1 and 6
Operation 2: Swap 3 and 4

Input: arr[] = {5, 54, 12, 63, 45}
Output: 45 54 12 63 5

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Initialize pointer i = 0 and j = N – 1 then swap the elements at these pointers and update i = i + 2 and j = j – 2. Repeat these steps while i < j. Finally print the updated array.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Utility function to print  ` `// the contents of an array ` `void` `printArr(``int` `arr[], ``int` `n) ` `{ ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``cout << arr[i] << ``" "``; ` `} ` ` `  `// Function to update the array ` `void` `UpdateArr(``int` `arr[], ``int` `n) ` `{ ` ` `  `    ``// Initialize the pointers ` `    ``int` `i = 0, j = n - 1; ` ` `  `    ``// While there are elements to swap ` `    ``while` `(i < j) { ` `        ``int` `temp = arr[i]; ` `        ``arr[i] = arr[j]; ` `        ``arr[j] = temp; ` ` `  `        ``// Update the pointers ` `        ``i += 2; ` `        ``j -= 2; ` `    ``} ` ` `  `    ``// Print the updated array ` `    ``printArr(arr, n); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 2, 3, 4, 5, 6 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``UpdateArr(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the above approach  ` `class` `GFG  ` `{ ` `     `  `    ``// Utility function to print  ` `    ``// the contents of an array  ` `    ``static` `void` `printArr(``int` `arr[], ``int` `n)  ` `    ``{  ` `        ``for` `(``int` `i = ``0``; i < n; i++)  ` `            ``System.out.print(arr[i] + ``" "``);  ` `    ``}  ` `     `  `    ``// Function to update the array  ` `    ``static` `void` `UpdateArr(``int` `arr[], ``int` `n)  ` `    ``{  ` `     `  `        ``// Initialize the pointers  ` `        ``int` `i = ``0``, j = n - ``1``;  ` `     `  `        ``// While there are elements to swap  ` `        ``while` `(i < j)  ` `        ``{  ` `            ``int` `temp = arr[i];  ` `            ``arr[i] = arr[j];  ` `            ``arr[j] = temp;  ` `     `  `            ``// Update the pointers  ` `            ``i += ``2``;  ` `            ``j -= ``2``;  ` `        ``}  ` `     `  `        ``// Print the updated array  ` `        ``printArr(arr, n);  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{  ` `        ``int` `arr[] = { ``1``, ``2``, ``3``, ``4``, ``5``, ``6` `};  ` `        ``int` `n = arr.length;  ` `     `  `        ``UpdateArr(arr, n);  ` `    ``} ` `}  ` ` `  `// This code is contributed by AnkitRai01 `

## Python3

 `# Python3 implementation of the approach ` ` `  `# Utility function to print ` `# the contents of an array ` `def` `printArr(arr, n): ` ` `  `    ``for` `i ``in` `range``(n): ` `        ``print``(arr[i], end ``=` `" "``); ` ` `  `# Function to update the array ` `def` `UpdateArr(arr, n): ` ` `  `    ``# Initialize the pointers ` `    ``i ``=` `0``; ` `    ``j ``=` `n ``-` `1``; ` ` `  `    ``# While there are elements to swap ` `    ``while` `(i < j): ` `        ``temp ``=` `arr[i]; ` `        ``arr[i] ``=` `arr[j]; ` `        ``arr[j] ``=` `temp; ` ` `  `        ``# Update the pointers ` `        ``i ``+``=` `2``; ` `        ``j ``-``=` `2``; ` `     `  `    ``# Print the updated array ` `    ``printArr(arr, n); ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``arr ``=` `[``1``, ``2``, ``3``, ``4``, ``5``, ``6` `]; ` `    ``n ``=` `len``(arr); ` ` `  `    ``UpdateArr(arr, n); ` ` `  `# This code is contributed by PrinciRaj1992 `

## C#

 `// C# implementation of the approach ` `using` `System; ` `     `  `class` `GFG  ` `{ ` `     `  `    ``// Utility function to print  ` `    ``// the contents of an array  ` `    ``static` `void` `printArr(``int` `[]arr, ``int` `n)  ` `    ``{  ` `        ``for` `(``int` `i = 0; i < n; i++)  ` `            ``Console.Write(arr[i] + ``" "``);  ` `    ``}  ` `     `  `    ``// Function to update the array  ` `    ``static` `void` `UpdateArr(``int` `[]arr, ``int` `n)  ` `    ``{  ` `     `  `        ``// Initialize the pointers  ` `        ``int` `i = 0, j = n - 1;  ` `     `  `        ``// While there are elements to swap  ` `        ``while` `(i < j)  ` `        ``{  ` `            ``int` `temp = arr[i];  ` `            ``arr[i] = arr[j];  ` `            ``arr[j] = temp;  ` `     `  `            ``// Update the pointers  ` `            ``i += 2;  ` `            ``j -= 2;  ` `        ``}  ` `     `  `        ``// Print the updated array  ` `        ``printArr(arr, n);  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main (String[] args)  ` `    ``{  ` `        ``int` `[]arr = { 1, 2, 3, 4, 5, 6 };  ` `        ``int` `n = arr.Length;  ` `     `  `        ``UpdateArr(arr, n);  ` `    ``} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```6 2 4 3 5 1
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.