Question: Write a program to read integer n, one by one, and store them in an array data index from 0 to n-1. Find the maximum value in data and put it in the center of the array, find the next largest value, and put it to its right, then the next largest and place it to its left, and so on.
In this post, I will share the program to rearrange array elements in a specified order.
For example:-
original array = 1,2,3,4,5
rearranged array = 1,3,5,4,2
working:
Since 5 is the largest element, it is placed in the middle, and 4 is the 2nd largest element, so we place it to its right. Now the 3rd largest element is 3, so we place it to its left. And this process continues till the last element.
Sample Input and Output:
Input 1:
Enter number of Integers:5
Enter Value of Integer 1:1
Enter Value of Integer 2:2
Enter Value of Integer 3:3
Enter Value of Integer 4:4
Enter Value of Integer 5:5
Output 1:
Original array:1,2,3,4,5
Rearranged array:1 3 5 4 2
Input 2:
Enter number of Integers:6
Enter Value of Integer 1:1
Enter Value of Integer 2:2
Enter Value of Integer 3:3
Enter Value of Integer 4:4
Enter Value of Integer 5:5
Enter Value of Integer 6:6
Output 2:
Original array:1,2,3,4,5,6
Rearranged array:2 4 6 5 3 1
Code :
Output Window:
If you have any doubts/questions related to Program, or if you want to give any suggestions, or want an explanation of code, feel free to comment below. I hope that my code is understandable. Please Subscribe to Programming Chaska for the latest programming updates and also to inspire me to write such unique and different programs.
Thank You for Visiting 😄
If you have any doubts/questions related to Program, or if you want to give any suggestions, or want an explanation of code, feel free to comment below. I hope that my code is understandable. Please Subscribe to Programming Chaska for the latest programming updates and also to inspire me to write such unique and different programs.
Thank You for Visiting 😄
Thank You for Visiting 😄
0 Comments