Open In App

Program to print an array in Pendulum Arrangement

Last Updated : 16 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Write a program to input a list of integers in an array and arrange them in a way similar to the to-and-fro movement of a Pendulum.

  • The minimum element out of the list of integers, must come in center position of 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 : 1   3   2   5   4
Output :5   3   1   2   4
Explanation: 
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 : 11   12   31   14   5
Output :31   12   5   11   14

The idea is to sort the array first. Once the array is sorted, use an auxiliary array to store elements one by one. 

Implementation:

C++




// C++ program for pendulum arrangement of numbers
#include <bits/stdc++.h>
using namespace std;
  
// Prints pendulum arrangement of arr[]
void pendulumArrangement(int arr[], int n)
{
    // sorting the elements
    sort(arr, arr+n);
  
    // Auxiliary array to store output
    int op[n];
  
    // calculating the middle index
    int mid = (n-1)/2;
  
    // storing the minimum element in the middle
    // i is index for output array and j is for
    // input array.
    int j = 1, i = 1;
    op[mid] = arr[0];
    for (i = 1; i <= mid; i++)
    {
        op[mid+i] = arr[j++];
        op[mid-i] = arr[j++];
    }
  
    // adjustment for when no. of elements is even
    if (n%2 == 0)
        op[mid+i] = arr[j];
  
  
    // Printing the pendulum arrangement
    cout << "Pendulum arrangement:" << endl;
    for (i = 0 ; i < n; i++)
        cout << op[i] << " ";
  
    cout << endl;
}
  
// Driver function
int main()
{
    //input Array
    int arr[] = {14, 6, 19, 21, 12};
  
    // calculating the length of array A
    int n = sizeof(arr)/sizeof(arr[0]);
  
    // calling pendulum function
    pendulumArrangement(arr, n);
  
    return 0;
}


Java




// Java program for pendulum arrangement of numbers
  
import java.util.Arrays;
  
class Test
{
    // Prints pendulum arrangement of arr[]
    static void pendulumArrangement(int arr[], int n)
    {
        // sorting the elements
        Arrays.sort(arr);
       
        // Auxiliary array to store output
        int op[] = new int[n];
       
        // calculating the middle index
        int mid = (n-1)/2;
       
        // storing the minimum element in the middle
        // i is index for output array and j is for
        // input array.
        int j = 1, i = 1;
        op[mid] = arr[0];
        for (i = 1; i <= mid; i++)
        {
            op[mid+i] = arr[j++];
            op[mid-i] = arr[j++];
        }
       
        // adjustment for when no. of elements is even
        if (n%2 == 0)
            op[mid+i] = arr[j];
       
       
        // Printing the pendulum arrangement
        System.out.println("Pendulum arrangement:");
        for (i = 0 ; i < n; i++)
            System.out.print(op[i] + " ");
       
        System.out.println();
    }
      
    // Driver method
    public static void main(String[] args) 
    {
        //input Array
        int arr[] = {14, 6, 19, 21, 12};
         
        // calling pendulum function
        pendulumArrangement(arr, arr.length);
    }
}


Python3




# Python 3 program for pendulum
# arrangement of numbers
  
# Prints pendulum arrangement of arr[]
def pendulumArrangement(arr, n):
  
    # sorting the elements
    arr.sort()
  
    # Auxiliary array to store output
    op = [0] * n
  
    # calculating the middle index
    mid = int((n-1)/2)
  
    # storing the minimum
        # element in the middle
    # i is index for output
        # array and j is for
    # input array.
    j = 1
    i = 1
    op[mid] = arr[0]
    for i in range(1,mid+1):
      
        op[mid+i] = arr[j]
        j+=1
        op[mid-i] = arr[j]
        j+=1
      
  
    # adjustment for when no.
        # of elements is even
    if (int(n%2) == 0):
        op[mid+i] = arr[j]
  
  
    # Printing the pendulum arrangement
    print("Pendulum arrangement:")
    for i in range(0,n):
        print(op[i],end=" ")
  
# Driver function
# input Array
arr = [14, 6, 19, 21, 12]
  
# calculating the length of array A
n = len(arr)
  
# calling pendulum function
pendulumArrangement(arr, n)
  
# This code is contributed by
# Smitha Dinesh Semwal


C#




// C# program for pendulum 
// arrangement of numbers
using System;
  
class Test {
      
    // Prints pendulum arrangement of arr[]
    static void pendulumArrangement(int []arr, 
                                    int n)
    {
          
        // sorting the elements
        Array.Sort(arr);
      
        // Auxiliary array to store output
        int []op = new int[n];
      
        // calculating the middle index
        int mid = (n - 1) / 2;
      
        // storing the minimum element in 
        // the middle i is index for output 
        // array and j is for input array.
        int j = 1, i = 1;
        op[mid] = arr[0];
        for (i = 1; i <= mid; i++)
        {
            op[mid + i] = arr[j++];
            op[mid - i] = arr[j++];
        }
      
        // adjustment for when no. 
        // of elements is even
        if (n % 2 == 0)
            op[mid + i] = arr[j];
      
      
        // Printing the pendulum arrangement
        Console.Write("Pendulum arrangement:");
        for (i = 0 ; i < n; i++)
            Console.Write(op[i] + " ");
      
        Console.WriteLine();
    }
      
    // Driver code
    public static void Main() 
    {
          
        //input Array
        int []arr = {14, 6, 19, 21, 12};
          
        // calling pendulum function
        pendulumArrangement(arr, arr.Length);
    }
}
  
// This code is contributed by Nitin Mittal.


PHP




<?php
// PHP program for pendulum 
// arrangement of numbers
  
// Prints pendulam arrangement of arr[]
function pendulumArrangement($arr, $n)
{
      
    // sorting the elements
    sort($arr, $n);
    sort($arr); 
  
    // Auxiliary array to
    // store output
    $op[$n] = NULL;
  
    // calculating the
    // middle index
    $mid = floor(($n - 1) / 2);
  
    // storing the minimum 
    // element in the middle
    // i is index for output
    // array and j is for
    // input array.
    $j = 1;
    $i = 1;
    $op[$mid] = $arr[0];
    for ($i = 1; $i <= $mid; $i++)
    {
        $op[$mid + $i] = $arr[$j++];
        $op[$mid - $i] = $arr[$j++];
    }
  
    // adjustment for when no. 
    // of elements is even
    if ($n % 2 == 0)
        $op[$mid + $i] = $arr[$j];
  
  
    // Printing the pendulum
    // arrangement
    echo "Pendulum arrangement:" ;
    for ($i = 0 ; $i < $n; $i++)
        echo $op[$i], " ";
  
    echo "\n";
}
  
    // Driver Code
    //input Array
    $arr = array(14, 6, 19, 21, 12);
  
    // calculating the length 
    // of array A
    $n = sizeof($arr);
  
    // calling pendulum function
    pendulumArrangement($arr, $n);
      
// This code is contributed by nitin mittal. 
?>


Javascript




<script>
      // JavaScript program for pendulum 
      // arrangement of numbers
  
      // Prints pendulam arrangement of arr[]
      function pendulumArrangement(arr, n) 
      {
          
        // sorting the elements
        arr.sort(function (a, b) {
          return a - b;
        });
  
        // Auxiliary array to store output
        var op = [...Array(n)];
  
        // calculating the middle index
        var mid = parseInt((n - 1) / 2);
  
        // storing the minimum element in the middle
        // i is index for output array and j is for
        // input array.
        var j = 1,
          i = 1;
        op[mid] = arr[0];
        for (i = 1; i <= mid; i++) {
          op[mid + i] = arr[j++];
          op[mid - i] = arr[j++];
        }
  
        // adjustment for when no. of elements is even
        if (n % 2 == 0) op[mid + i] = arr[j];
  
        // Printing the pendulum arrangement
        document.write("Pendulum arrangement:<br>");
        for (i = 0; i < n; i++) 
        document.write(op[i] + "  ");
  
        document.write("<br>");
      }
  
      // Driver function
        
      //input Array
      var arr = [14, 6, 19, 21, 12];
        
      // calculating the length of array A
      var n = arr.length;
        
      // calling pendulum function
      pendulumArrangement(arr, n);
    </script>


Output

Pendulum arrangement:
21 14 6 12 19 

Time Complexity: O(n*log(n)) where n is the size of the array.
Auxiliary Space: O(n)

 



Similar Reads

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 h
9 min read
Maximum height of triangular arrangement of array values
Given an array, we need to find the maximum height of the triangle which we can form, from the array values such that every (i+1)th level contain more elements with the larger sum from the previous level. Examples: Input : a = { 40, 100, 20, 30 }Output : 2Explanation : We can have 100 and 20 at the bottom level and either 40 or 30 at the upper leve
8 min read
Find arrangement of Array such that prefix OR is maximum for each index
Given an array arr[] consisting of N non-negative integers. The task is to find a rearrangement of the array such that the prefix OR for each index is maximum among all possible arrangements. Examples: Input: N = 4, arr[] = [1, 2, 4, 8] Output: [8, 4, 2, 1]Explanation: Prefix of the rearranged array is [8, 8 | 4, 8 | 4 | 2, 8 | 4 | 2 | 1] = [8, 12,
9 min read
Adjacent element arrangement in Circular Array
Given a circular array arr[] of size N where the last element is adjacent to the first element, the task is to arrange an array where the adjacent element's absolute difference is 1. Return "YES" Otherwise "NO". Examples: Input: N = 6, arr[] = {1, 1, 2, 2, 2, 3}Output: YESExplanation: {2, 1, 2, 1, 2, 3} is one of the possible rearrangements. Approa
5 min read
Find the arrangement of queue at given time
n people are standing in a queue to buy entry ticket for the carnival. People present there strongly believe in chivalry. Therefore, at time = t, if a man at position x, finds a woman standing behind him then he exchanges his position with her and therefore, at time = t+1, woman is standing at position x while man is standing behind her. Given the
6 min read
Find if neat arrangement of cups and shelves can be made
Given three different types of cups (a[]) and saucers (b[]), and n number of shelves, find if neat arrangement of cups and shelves can be made. Arrangement of the cups and saucers will be neat if it follows the below rules: No shelf can contain both cups and saucersThere can be no more than 5 cups in any shelfThere can be no more than 10 saucers in
7 min read
Seating arrangement of n boys and girls alternatively around a round table
There are n boys and n (n &lt; 10) girls are to be seated around a round table, in a circle. The task is to find the number of ways in which n boys and n girls can sit alternatively around a round table. Examples: Input: n = 5 Output: 2880 Input: n = 1Output: 1Explanation: There is only 1 boy and 1 girl.So there is only one possible arrangement App
5 min read
Arrangement of words without changing the relative position of vowel and consonants
Given a word of length less than 10, the task is to find a number of ways in which it can be arranged without changing the relative position of vowels and consonants. Examples: Input: "GEEKS" Output: 6 Input: "COMPUTER" Output: 720 Approach Count the vowels and consonants in the wordNow find total number of ways to arrange vowel onlyThen find ways
9 min read
Arrangement of the characters of a word such that all vowels are at odd places
Given a string 'S' containing vowels and consonants of lowercase English alphabets. The task is to find the number of ways in which the characters of the word can be arranged such that the vowels occupy only the odd positions. Examples: Input: geeks Output: 36 [Tex]3_P_2 \times 3_P_3 = 36 [/Tex] Input: publish Output: 1440 [Tex]4_P_2 \times 5_P_5 =
9 min read
Seating arrangement of N boys sitting around a round table such that two particular boys sit together
There are N boys which are to be seated around a round table. The task is to find the number of ways in which N boys can sit around a round table such that two particular boys sit together.Examples: Input: N = 5 Output: 12 2 boy can be arranged in 2! ways and other boys can be arranged in (5 - 2)! (2 is subtracted because the previously selected tw
3 min read
Practice Tags :