Swap Alternate Boundary Pairs - GeeksforGeeks

26 Feb, 2021
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 4Input: 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++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Utility function to print// the contents of an arrayvoid printArr(int arr[], int n){    for (int i = 0; i < n; i++)        cout << arr[i] << " ";} // Function to update the arrayvoid 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 codeint main(){    int arr[] = { 1, 2, 3, 4, 5, 6 };    int n = sizeof(arr) / sizeof(arr[0]);     UpdateArr(arr, n);     return 0;}
// Java implementation of the above approachclass 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 implementation of the approach # Utility function to print# the contents of an arraydef printArr(arr, n):     for i in range(n):        print(arr[i], end = " "); # Function to update the arraydef 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 codeif __name__ == '__main__':    arr = [1, 2, 3, 4, 5, 6 ];    n = len(arr);     UpdateArr(arr, n); # This code is contributed by PrinciRaj1992
// C# implementation of the approachusing 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
<script> // JavaScript implementation of the approach // Utility function to print// the contents of an arrayfunction printArr(arr, n){    for (let i = 0; i < n; i++)        document.write(arr[i] + " ");} // Function to update the arrayfunction UpdateArr(arr, n){     // Initialize the pointers    let i = 0, j = n - 1;     // While there are elements to swap    while (i < j) {        let 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     let arr = [ 1, 2, 3, 4, 5, 6 ];    let n = arr.length;;     UpdateArr(arr, n); // This code is contributed by Surbhi Tyagi </script>
6 2 4 3 5 1
 
ankthon
29AjayKumar
princiraj1992
Akanksha_Rai
surbhityagi15
Arrays
Sorting
Arrays
Sorting
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count pairs with given sum
Chocolate Distribution Problem
Window Sliding Technique
Reversal algorithm for array rotation
Next Greater Element