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.
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.
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 email@example.com. 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Program to print an array in Pendulum Arrangement with constant space
- Maximum height of triangular arrangement of array values
- Program to print Sum Triangle for a given array
- C++ Program to print an Array using Recursion
- Program to print Sum of even and odd elements in an array
- Program to print product of even and odd indexed elements in an Array
- Program to print Lower triangular and Upper triangular matrix of an array
- Find if neat arrangement of cups and shelves can be made
- Print modified array after multiple array range increment operations
- Print all maximal increasing contiguous sub-array in an array
- Print elements of an array according to the order defined by another array | set 2
- Program to print Hut
- Program to print 2D shapes
- Program to print the pattern 'G'
- Program to print the pattern "GFG"
- Program to print the Sum of series -1 + 2 + 11 + 26 + 47 +.....
- Program to print DNA sequence
- Program to print the pattern ‘D’
- Program to print pattern
- C++ program to print all Even and Odd numbers from 1 to N
Improved By : nitin mittal