Program to print an array in Pendulum Arrangement with constant space

Given an array arr[] of integers, the task is to arrange them in a way similar to the to-and-fro movement of a Pendulum without using any extra space.
Pendulum Arrangement:Â
Â
- The minimum element out of the list of integers must come in the center position of the array.
- The number in the ascending order next to the minimum, goes to the right, the next higher number goes to the left of minimum number and it continues.
- As higher numbers are reached, one goes to one side in a to-and-fro manner similar to that of a Pendulum.
Examples:Â
Â
Input: arr[] = {2, 3, 5, 1, 4}Â
Output: 5 3 1 2 4Â
The minimum element is 1, so it is moved to the middle.Â
The next higher element 2 is moved to the right of theÂ
middle element while the next higher element 3 isÂ
moved to the left of the middle element andÂ
this process is continued.
Input: arr[] = {11, 2, 4, 55, 6, 8}Â
Output: 11 6 2 4 8 55Â
Â
Â
Approach: An approach which uses an auxiliary array has been discussed in this article. Here’s an approach without using extra space:Â
Â
- Sort the given array.
- Move all the odd position element in the right side of the array.
- Reverse the element from 0 to (n-1)/2 position of the array.
Â
For example, let arr[] = {2, 3, 5, 1, 4}Â
Sorted array will be arr[] = {1, 2, 3, 4, 5}.Â
After moving all odd index position elements to the right,Â
arr[] = {1, 3, 5, 2, 4} (1 and 3 are the odd index positions)Â
After reversing elements from 0 to (n – 1) / 2,Â
arr[] = {5, 3, 1, 2, 4} which is the required array.Â
Â
Below is the implementation of the above approach:Â
Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to print the Pendulum// arrangement of the given arrayvoid pendulumArrangement(int arr[], int n){    // Sort the array    sort(arr, arr + n);Â
    int odd, temp, in, pos;Â
    // pos stores the index of    // the last element of the array    pos = n - 1;Â
    // odd stores the last odd index in the array    if (n % 2 == 0)        odd = n - 1;    else        odd = n - 2;Â
    // Move all odd index positioned    // elements to the right    while (odd > 0) {        temp = arr[odd];        in = odd;Â
        // Shift the elements by one position        // from odd to pos        while (in != pos) {            arr[in] = arr[in + 1];            in++;        }        arr[in] = temp;        odd = odd - 2;        pos = pos - 1;    }Â
    // Reverse the element from 0 to (n - 1) / 2    int start = 0, end = (n - 1) / 2;Â
    for (; start < end; start++, end--) {        temp = arr[start];        arr[start] = arr[end];        arr[end] = temp;    }Â
    // Printing the pendulum arrangement    for (int i = 0; i < n; i++)        cout << arr[i] << " ";}Â
// Driver codeint main(){Â Â Â Â int arr[] = { 11, 2, 4, 55, 6, 8 };Â Â Â Â int n = sizeof(arr) / sizeof(arr[0]);Â
    pendulumArrangement(arr, n);Â
    return 0;} |
Java
// Java implementation of the approachimport java.util.Arrays;import java.io.*;Â
class GFG {Â
    // Function to print the Pendulum    // arrangement of the given array    static void pendulumArrangement(int arr[], int n)    {        // Sort the array        // sort(arr, arr + n);Â
        Arrays.sort(arr);Â
        int odd, temp, in, pos;Â
        // pos stores the index of        // the last element of the array        pos = n - 1;Â
        // odd stores the last odd index in the array        if (n % 2 == 0)            odd = n - 1;        else            odd = n - 2;Â
        // Move all odd index positioned        // elements to the right        while (odd > 0) {            temp = arr[odd];            in = odd;Â
            // Shift the elements by one position            // from odd to pos            while (in != pos) {                arr[in] = arr[in + 1];                in++;            }            arr[in] = temp;            odd = odd - 2;            pos = pos - 1;        }Â
        // Reverse the element from 0 to (n - 1) / 2        int start = 0, end = (n - 1) / 2;Â
        for (; start < end; start++, end--) {            temp = arr[start];            arr[start] = arr[end];            arr[end] = temp;        }Â
        // Printing the pendulum arrangement        for (int i = 0; i < n; i++)            System.out.print(arr[i] + " ");    }Â
    // Driver code    public static void main(String[] args)    {Â
        int arr[] = { 11, 2, 4, 55, 6, 8 };        int n = arr.length;Â
        pendulumArrangement(arr, n);    }}Â
// This code is contributed by akt_mit |
Python3
# Python 3 implementation of the approachÂ
# Function to print the Pendulum# arrangement of the given arraydef pendulumArrangement(arr, n):         # Sort the array    arr.sort(reverse = False)Â
    # pos stores the index of    # the last element of the array    pos = n - 1Â
    # odd stores the last odd index in the array    if (n % 2 == 0):        odd = n - 1    else:        odd = n - 2Â
    # Move all odd index positioned    # elements to the right    while (odd > 0):        temp = arr[odd]        in1 = oddÂ
        # Shift the elements by one position        # from odd to pos        while (in1 != pos):            arr[in1] = arr[in1 + 1]            in1 += 1Â
        arr[in1] = temp        odd = odd - 2        pos = pos - 1Â
    # Reverse the element from 0 to (n - 1) / 2    start = 0    end = int((n - 1) / 2)Â
    while(start < end):        temp = arr[start]        arr[start] = arr[end]        arr[end] = temp        start += 1        end -= 1Â
    # Printing the pendulum arrangement    for i in range(n):        print(arr[i], end = " ")Â
# Driver codeif __name__ == '__main__':Â Â Â Â arr = [11, 2, 4, 55, 6, 8]Â Â Â Â n = len(arr)Â
    pendulumArrangement(arr, n)Â
# This code is contributed by# Surendra_Gangwar |
C#
// C# implementation of the approachusing System; Â
class GFG {Â
    // Function to print the Pendulum    // arrangement of the given array    static void pendulumArrangement(int[] arr, int n)    {        // Sort the array        // sort(arr, arr + n);Â
        Array.Sort(arr);Â
        int odd, temp, p, pos;Â
        // pos stores the index of        // the last element of the array        pos = n - 1;Â
        // odd stores the last odd index in the array        if (n % 2 == 0)            odd = n - 1;        else            odd = n - 2;Â
        // Move all odd index positioned        // elements to the right        while (odd > 0)         {            temp = arr[odd];            p = odd;Â
            // Shift the elements by one position            // from odd to pos            while (p != pos)            {                arr[p] = arr[p + 1];                p++;            }            arr[p] = temp;            odd = odd - 2;            pos = pos - 1;        }Â
        // Reverse the element from 0 to (n - 1) / 2        int start = 0, end = (n - 1) / 2;Â
        for (; start < end; start++, end--)         {            temp = arr[start];            arr[start] = arr[end];            arr[end] = temp;        }Â
        // Printing the pendulum arrangement        for (int i = 0; i < n; i++)            Console.Write(arr[i] + " ");    }Â
    // Driver code    public static void Main()    {Â
        int[] arr = { 11, 2, 4, 55, 6, 8 };        int n = arr.Length;Â
        pendulumArrangement(arr, n);    }}Â
// This code is contributed by ChitraNayal |
PHP
<?php// PHP implementation of the approach Â
// Function to print the Pendulum // arrangement of the given array function pendulumArrangement($arr, $n) {     // Sort the array     sort($arr) ; Â
    // pos stores the index of     // the last element of the array     $pos = $n - 1; Â
    // odd stores the last odd index in the array     if ($n % 2 == 0)         $odd = $n - 1;     else        $odd = $n - 2; Â
    // Move all odd index positioned     // elements to the right     while ($odd > 0)    {         $temp = $arr[$odd];         $in = $odd; Â
        // Shift the elements by one position         // from odd to pos         while ($in != $pos)         {             $arr[$in] = $arr[$in + 1];             $in++;         }         $arr[$in] = $temp;         $odd = $odd - 2;         $pos = $pos - 1;     } Â
    // Reverse the element from 0 to (n - 1) / 2     $start = 0;    $end = floor(($n - 1) / 2); Â
    for (; $start < $end; $start++, $end--)     {         $temp = $arr[$start];         $arr[$start] = $arr[$end];         $arr[$end] = $temp;     } Â
    // Printing the pendulum arrangement     for ($i = 0; $i < $n; $i++)         echo $arr[$i], " "; } Â
// Driver code $arr = array( 11, 2, 4, 55, 6, 8 ); $n = count($arr); Â
pendulumArrangement($arr, $n); Â
// This code is contributed by AnkitRai01Â
?> |
Javascript
<script>    // Javascript implementation of the approach         // Function to print the Pendulum    // arrangement of the given array    function pendulumArrangement(arr, n)    {        // Sort the array        // sort(arr, arr + n);           arr.sort(function(a, b){return a - b});           let odd, temp, p, pos;           // pos stores the index of        // the last element of the array        pos = n - 1;           // odd stores the last odd index in the array        if (n % 2 == 0)            odd = n - 1;        else            odd = n - 2;           // Move all odd index positioned        // elements to the right        while (odd > 0)         {            temp = arr[odd];            p = odd;               // Shift the elements by one position            // from odd to pos            while (p != pos)            {                arr[p] = arr[p + 1];                p++;            }            arr[p] = temp;            odd = odd - 2;            pos = pos - 1;        }           // Reverse the element from 0 to (n - 1) / 2        let start = 0, end = parseInt((n - 1) / 2, 10);           for (; start < end; start++, end--)         {            temp = arr[start];            arr[start] = arr[end];            arr[end] = temp;        }           // Printing the pendulum arrangement        for (let i = 0; i < n; i++)            document.write(arr[i] + " ");    }         let arr = [ 11, 2, 4, 55, 6, 8 ];    let n = arr.length;Â
    pendulumArrangement(arr, n);     </script> |
11 6 2 4 8 55
Â
Time Complexity: O(n2)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



