- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Rearrange an Array in Maximum Minimum Form using C++

We are given a sorted array. We need to arrange this array in maximum, minimum form, i.e., the first element is the maximum element, the second element is the minimum element, the third element is 2nd maximum element, the fourth element is 2nd minimum element, and so on, for example −

Input : arr[ ] = { 10, 20, 30, 40, 50, 60 } Output : { 60, 10, 50, 20, 40, 30 } Explanation : array is rearranged in the form { 1st max, 1st min, 2nd max, 2nd min, 3rd max, 3rd min } Input : arr [ ] = { 15, 17, 19, 23, 36, 67, 69 } Output : { 69, 15, 67, 17, 36, 19, 23 }

There is single approach to rearrange an array in the maximum and minimum form −

## Approach to find The Solution

There is single approach to rearrange an array in the maximum and minimum form −

## Two-pointer Approach

Use two variables, min, and max, here which will point to the maximum and minimum element and also create a new empty array of the same size to store the rearranged array. Now iterate over the array, and if the iterative element is at even index, then add arr[max] element to the empty array and decrement max by 1. if the element is at an odd index, then add arr[min] element to the empty array and increment min by 1. Do this until max becomes smaller than min.

## Example

#include <bits/stdc++.h> using namespace std; int main () { int arr[] = { 1, 2, 3, 4, 5, 6 }; int n = sizeof (arr) / sizeof (arr[0]); // creating a new array to store the rearranged array. int final[n]; // pointing variables to initial and final element index. int min = 0, max = n - 1; int count = 0; // iterating over the array until max is less than or equals to max. for (int i = 0; min <= max; i++) { // if count is even then store max index element if (count % 2 == 0) { final[i] = arr[max]; max--; } // store min index element else { final[i] = arr[min]; min++; } count++; } // printing the final rearranged array. for (int i = 0; i < n; i++) cout << final[ i ] << " "; return 0; }

## Output

6 1 5 2 4 3

## Explanation of the Above Code

- Variables are initialised as min=0 and max = array_length(n) - 1.
- for (int i = 0; min <= max; i++) to iterate over the array until max becomes greater than min.
- If the count is odd, then (max)the element is added in the final array, and variable max is decremented by 1.
- Suppose the count is even then(min). In that case, the element is added to the final array, and the variable min is incremented by 1.
- Finally, the resulting array is stored in the final[ ] array.

## Conclusion

In this article, we discussed the solution to rearrange the given array to max-min form. We discussed the approach for the solution and solved it with an optimistic solution with time complexity O(n). We also write a C++ program for the same. Similarly, we can write this program in any other language like C, Java, Python, etc. We hope you find this article helpful.

- Related Articles
- C++ program to rearrange an array in maximum minimum form
- Rearrange an array in maximum minimum form by JavaScript
- Maximum and minimum of an array using minimum number of comparisons in C
- Rearrange an Array to Maximize i*arr[i] using C++
- How to find the minimum and maximum element of an Array using STL in C++?
- C Program to Minimum and Maximum prime numbers in an array
- Rearrange An Array In Order – Smallest, Largest, 2nd Smallest, 2nd Largest,. Using C++
- C# program to find maximum and minimum element in an array\n
- Maximum adjacent difference in an array in its sorted form in C++
- Program to find the minimum (or maximum) element of an array in C++
- Rearrange an array such that arr[i] = i in C++
- Find minimum in an array without using Relational Operators in C++
- Rearrange characters to form palindrome if possible in C++
- Find maximum in an array without using Relational Operators in C++
- Returning an array with the minimum and maximum elements JavaScript