# Rearrange given array in maximum minimum form

0
258 ## Rearrange the given sorted array of positive integers, such that alternative elements are ith max and ith min.

Array = maximum value of the array
Array = minimum value of the array
Array = second maximum value of the array
Array = second minimum value of the array
Array = 3rd maximum value of the array  …….. and so on.

### Example ## Algorithm

Step 1 : Create an auxiliary dummy array to store the modified array.

Step 2 : Initialize low and high as the corner indices of the array.

Step 3 : For copying alternative max and min store a variable X and initialize True, if X = True we copy max value and if X = False we copy min value.

Step 4 : Run a loop to copy elements into auxiliary array. After copying an element into the new array reverse the value of X.

Step 5 : If X is True copy element from end and move backward, if X is False copy element from front and move forward.

Step 6 : copy the auxiliary array into the given array.

Step 7: Print the array, the elements will be rearranged.

### Algorithm working  ## C++ Program

``````#include <bits/stdc++.h>
using namespace std;

//function to rearrange the given sorted array in maximum minimum form
void RearrangeMaxMin(int array[], int N)
{
int temp[N];//auxilary dummy array with all zeroes
for (int i=0; i<N; i++)
{
temp[i] = 0;
}
int low=0, high=N-1; // corner indices of the array
int X = true; // indication for which element should be copied
//copying elements into the temp array
//according to the given condition
for (int i=0; i<N; i++)
{
if(X)
{
temp[i] = array[high--];
}
else
{
temp[i] = array[low++];
}
X = !X;
}
for (int i=0; i<N; i++)
{
array[i] = temp[i];
}
}
//function to print the given array
int PrintArray(int array[],int N)
{
for (int i=0; i<N; i++)
{
cout << array[i] << " ";
}
}
// main function
int main()
{
int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int N = sizeof(array)/sizeof(array);
cout << "Input array\n";
PrintArray(array,N);
RearrangeMaxMin(array,N);
cout << "\nOutput array\n";
PrintArray(array,N);
return 0;
}``````