Program to print an array in Pendulum Arrangement

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.

C++

// C++ program for pendulum arrangement of numbers
#include <bits/stdc++.h>
using namespace std;

// Prints pendulam 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);
    }
}


Output:

Pendulum arrangement:
21 14 6 12 19

This article is contributed by Nitin Agarwal. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:







Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.