# 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

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`

## Javascript

 ``

Output:

`6 2 4 3 5 1`

Time Complexity: O(n)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next