
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to rearrange an array in maximum minimum form
We are given an integer array which can be arranged in sorted/unsorted manner. The task is to first sort the array if the values are unsorted then arrange the array in such a manner that the first element of array will be the maximum value, second will be the minimum value, third will be the second largest value, fourth will be the second minimest value and so on.
Let us see various input output scenarios for this −
Input − int arr[] = {7, 5, 2, 3, 4, 9, 10, 5 }
Output − Array before Arrangement: 2 3 4 5 5 7 9 10 Rearrangement of an array in maximum minimum form is: 10 2 9 3 7 4 5 5
Explanation − we are given an integer type array containing values as {7, 5, 2, 3, 4, 9, 10, 5 }. Firstly we will sort an array and it will be {2 3 4 5 5 7 9 10 }. Secondly, arrange the largest element at arr[0] i.e. 10 then smallest element at arr[1] i.e. 2 then second largest element at arr[2] i.e. 9 and so on. The final resultant array will be 10 2 9 3 7 4 5 5
Input − int arr[] = {2, 4, 1, 6, 7}
Output − Array before Arrangement: 1, 2, 4, 6, 7 Rearrangement of an array in maximum minimum form is: 7, 1, 6, 2, 4
Explanation − we are given an integer type array containing values as {2, 4, 1, 6, 7}. Firstly we will sort an array and it will be {1, 2, 4, 6, 7}. Secondly, arrange the largest element at arr[0] i.e. 7 then smallest element at arr[1] i.e. 1 then second largest element at arr[2] i.e. 6 and so on. The final resultant array will be 7, 1, 6, 2, 4
Approach used in the below program is as follows
Input an array of integer type elements and calculate the size of an array. Call the sort method of C++ STL by passing arr[] and size of an array to the function as an argument.
Print the array before arrangement and call the function Rearr_Max_Min(arr, size)
Inside the function Rearr_Max_Min(arr, size)
Declare a variable as max and set it with size - 1 and another variable as min and set it with 0. Declare a variable as max_val and set it with arr[size - 1] + 1.
Start loop FOR from i to 0 till i less than size. Inside the loop, check IF i % 2 = 0 then set arr[i] to arr[i] + (arr[max] % max_val) * max_val and decrement the max by 1.
Else, set arr[i] to arr[i] + (arr[min] % max_val) * max_val and increment the min by 1.
Start loop FOR from i to 0 till i less than size. Inside the loop, set arr[i] to arr[i] / max_val
Example
#include <bits/stdc++.h> using namespace std; void Rearr_Max_Min(int arr[], int size){ int max = size - 1; int min = 0; int max_val = arr[size - 1] + 1; for (int i = 0; i < size; i++){ if (i % 2 == 0){ arr[i] += (arr[max] % max_val) * max_val; max--; } else{ arr[i] += (arr[min] % max_val) * max_val; min++; } } for(int i = 0; i < size; i++){ arr[i] = arr[i] / max_val; } } int main(){ //input an array int arr[] = {7, 5, 2, 3, 4, 9, 10, 5 }; int size = sizeof(arr) / sizeof(arr[0]); //sort an array sort(arr, arr + size); //print the original Array after sorting cout<<"Array before Arrangement: "; for (int i = 0; i < size; i++){ cout << arr[i] << " "; } //calling the function to rearrange the array Rearr_Max_Min(arr, size); //print the array after rearranging the values cout<<"\nRearrangement of an array in maximum minimum form is: "; for(int i = 0; i < size; i++){ cout<< arr[i] << " "; } return 0; }
Output
If we run the above code it will generate the following Output
Array before Arrangement: 2 3 4 5 5 7 9 10 Rearrangement of an array in maximum minimum form is: 10 2 9 3 7 4 5 5
- Related Articles
- Rearrange an Array in Maximum Minimum Form using C++
- Rearrange an array in maximum minimum form by JavaScript
- C Program to Minimum and Maximum prime numbers in an array
- C# program to find maximum and minimum element in an array\n
- Program to find the minimum (or maximum) element of an array in C++
- Maximum and minimum of an array using minimum number of comparisons in C
- Maximum adjacent difference in an array in its sorted form in C++
- Rearrange an Array to Maximize i*arr[i] using C++
- Maximum product subset of an array in C++ program
- How to find the minimum and maximum element of an Array using STL in C++?
- C++ Program to Find Minimum Element in an Array using Linear Search
- Rearrange characters to form palindrome if possible in C++
- Rearrange an array such that arr[i] = i in C++
- C++ Program to Find Maximum Element in an Array using Binary Search
- Recursive Programs to find Minimum and Maximum elements of array in C++
