Program to print an array in Pendulum Arrangement
Last Updated :
16 Feb, 2023
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++
#include <bits/stdc++.h>
using namespace std;
void pendulumArrangement( int arr[], int n)
{
sort(arr, arr+n);
int op[n];
int mid = (n-1)/2;
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++];
}
if (n%2 == 0)
op[mid+i] = arr[j];
cout << "Pendulum arrangement:" << endl;
for (i = 0 ; i < n; i++)
cout << op[i] << " " ;
cout << endl;
}
int main()
{
int arr[] = {14, 6, 19, 21, 12};
int n = sizeof (arr)/ sizeof (arr[0]);
pendulumArrangement(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
class Test
{
static void pendulumArrangement( int arr[], int n)
{
Arrays.sort(arr);
int op[] = new int [n];
int mid = (n- 1 )/ 2 ;
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++];
}
if (n% 2 == 0 )
op[mid+i] = arr[j];
System.out.println( "Pendulum arrangement:" );
for (i = 0 ; i < n; i++)
System.out.print(op[i] + " " );
System.out.println();
}
public static void main(String[] args)
{
int arr[] = { 14 , 6 , 19 , 21 , 12 };
pendulumArrangement(arr, arr.length);
}
}
|
Python3
def pendulumArrangement(arr, n):
arr.sort()
op = [ 0 ] * n
mid = int ((n - 1 ) / 2 )
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
if ( int (n % 2 ) = = 0 ):
op[mid + i] = arr[j]
print ( "Pendulum arrangement:" )
for i in range ( 0 ,n):
print (op[i],end = " " )
arr = [ 14 , 6 , 19 , 21 , 12 ]
n = len (arr)
pendulumArrangement(arr, n)
|
C#
using System;
class Test {
static void pendulumArrangement( int []arr,
int n)
{
Array.Sort(arr);
int []op = new int [n];
int mid = (n - 1) / 2;
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++];
}
if (n % 2 == 0)
op[mid + i] = arr[j];
Console.Write( "Pendulum arrangement:" );
for (i = 0 ; i < n; i++)
Console.Write(op[i] + " " );
Console.WriteLine();
}
public static void Main()
{
int []arr = {14, 6, 19, 21, 12};
pendulumArrangement(arr, arr.Length);
}
}
|
PHP
<?php
function pendulumArrangement( $arr , $n )
{
sort( $arr , $n );
sort( $arr );
$op [ $n ] = NULL;
$mid = floor (( $n - 1) / 2);
$j = 1;
$i = 1;
$op [ $mid ] = $arr [0];
for ( $i = 1; $i <= $mid ; $i ++)
{
$op [ $mid + $i ] = $arr [ $j ++];
$op [ $mid - $i ] = $arr [ $j ++];
}
if ( $n % 2 == 0)
$op [ $mid + $i ] = $arr [ $j ];
echo "Pendulum arrangement:" ;
for ( $i = 0 ; $i < $n ; $i ++)
echo $op [ $i ], " " ;
echo "\n" ;
}
$arr = array (14, 6, 19, 21, 12);
$n = sizeof( $arr );
pendulumArrangement( $arr , $n );
?>
|
Javascript
<script>
function pendulumArrangement(arr, n)
{
arr.sort( function (a, b) {
return a - b;
});
var op = [...Array(n)];
var mid = parseInt((n - 1) / 2);
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++];
}
if (n % 2 == 0) op[mid + i] = arr[j];
document.write( "Pendulum arrangement:<br>" );
for (i = 0; i < n; i++)
document.write(op[i] + " " );
document.write( "<br>" );
}
var arr = [14, 6, 19, 21, 12];
var n = arr.length;
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...