# Rearrange an array in maximum minimum form | Set 1

Given a sorted array of positive integers, rearrange the array alternately i.e first element should be maximum value, second minimum value, third second max, fourth second min and so on.

Examples:

```Input  : arr[] = {1, 2, 3, 4, 5, 6, 7}
Output : arr[] = {7, 1, 6, 2, 5, 3, 4}

Input  : arr[] = {1, 2, 3, 4, 5, 6}
Output : arr[] = {6, 1, 5, 2, 4, 3}```

Expected time complexity is O(n).

The idea is use an auxiliary array. We maintain two pointers one to leftmost or smallest element and other to rightmost or largest element. We more both pointers toward each other and alternatively copy elements at these pointers to an auxiliary array. Finally we copy auxiliary array back to original array.

Below are programs based on above facts.

## C++

 `// C++ program to rearrange an array in minimum ` `// maximum form ` `#include ` `using` `namespace` `std; ` ` `  `// Prints max at first position, min at second position ` `// second max at third position, second min at fourth ` `// position and so on. ` `void` `rearrange(``int` `arr[], ``int` `n) ` `{ ` `    ``// Auxiliary array to hold modified array ` `    ``int` `temp[n]; ` ` `  `    ``// Indexes of smallest and largest elements ` `    ``// from remaining array. ` `    ``int` `small=0, large=n-1; ` ` `  `    ``// To indicate whether we need to copy rmaining ` `    ``// largest or remaining smallest at next position ` `    ``int` `flag = ``true``; ` ` `  `    ``// Store result in temp[] ` `    ``for` `(``int` `i=0; i

## Java

 `// Java program to rearrange an array in minimum ` `// maximum form ` ` `  `import` `java.util.Arrays; ` ` `  `public` `class` `GFG ` `{ ` `    ``// Prints max at first position, min at second position ` `    ``// second max at third position, second min at fourth ` `    ``// position and so on. ` `    ``static` `void` `rearrange(``int``[] arr, ``int` `n) ` `    ``{ ` `        ``// Auxiliary array to hold modified array ` `        ``int` `temp[] = ``new` `int``[n]; ` `     `  `        ``// Indexes of smallest and largest elements ` `        ``// from remaining array. ` `        ``int` `small=``0``, large=n-``1``; ` `     `  `        ``// To indicate whether we need to copy rmaining ` `        ``// largest or remaining smallest at next position ` `        ``boolean` `flag = ``true``; ` `     `  `        ``// Store result in temp[] ` `        ``for` `(``int` `i=``0``; i

## Python

 `# Python program to rearrange an array in minimum ` `# maximum form ` ` `  `# Prints max at first position, min at second position ` `# second max at third position, second min at fourth ` `# position and so on. ` `def` `rearrange(arr, n): ` `    ``# Auxiliary array to hold modified array ` `    ``temp ``=` `n``*``[``None``] ` ` `  `    ``# Indexes of smallest and largest elements ` `    ``# from remaining array. ` `    ``small,large ``=``0``,n``-``1` ` `  `    ``# To indicate whether we need to copy rmaining ` `    ``# largest or remaining smallest at next position ` `    ``flag ``=` `True` ` `  `    ``# Store result in temp[] ` `    ``for` `i ``in` `range``(n): ` `        ``if` `flag ``is` `True``: ` `            ``temp[i] ``=` `arr[large] ` `            ``large ``-``=` `1` `        ``else``: ` `            ``temp[i] ``=` `arr[small] ` `            ``small ``+``=` `1` ` `  `        ``flag ``=` `bool``(``1``-``flag) ` ` `  `    ``# Copy temp[] to arr[] ` `    ``for` `i ``in` `range``(n): ` `        ``arr[i] ``=` `temp[i] ` `    ``return` `arr ` ` `  `# Driver program to test above function ` `arr ``=` `[``1``, ``2``, ``3``, ``4``, ``5``, ``6``, ``7``, ``8``, ``9``] ` `n ``=` `len``(arr) ` `print``(``"Original Array"``) ` `print``(arr) ` `print``(``"Modified Array"``) ` `print``(rearrange(arr, n)) ` ` `  `# This code is contributed by Pratik Chhajer `

## C#

 `// C# program to rearrange  ` `// an array in minimum  ` `// maximum form ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `// Prints max at first position,  ` `// min at second position second  ` `// max at third position, second  ` `// min at fourth position and so on. ` `static` `void` `rearrage(``int``[] arr,  ` `                     ``int` `n) ` `{ ` `    ``// Auxiliary array to  ` `    ``// hold modified array ` `    ``int` `[]temp = ``new` `int``[n]; ` ` `  `    ``// Indexes of smallest  ` `    ``// and largest elements ` `    ``// from remaining array. ` `    ``int` `small = 0, large = n - 1; ` ` `  `    ``// To indicate whether we  ` `    ``// need to copy rmaining ` `    ``// largest or remaining  ` `    ``// smallest at next position ` `    ``bool` `flag = ``true``; ` ` `  `    ``// Store result in temp[] ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``if` `(flag) ` `            ``temp[i] = arr[large--]; ` `        ``else` `            ``temp[i] = arr[small++]; ` ` `  `        ``flag = !flag; ` `    ``} ` ` `  `    ``// Copy temp[] to arr[] ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``arr[i] = temp[i]; ` `} ` ` `  `// Driver Code ` `static` `void` `Main() ` `{ ` `    ``int``[] arr = {1, 2, 3, 4,  ` `                 ``5, 6, 7, 8, 9}; ` ` `  `    ``Console.WriteLine(``"Original Array"``); ` `    ``for``(``int` `i = 0; i < arr.Length; i++) ` `        ``Console.Write(arr[i] + ``" "``); ` ` `  `    ``rearrage(arr, arr.Length);      ` ` `  `    ``Console.WriteLine(````" Modified Array"````); ` `    ``for``(``int` `i = 0; i < arr.Length; i++) ` `        ``Console.Write(arr[i] + ``" "``); ` `} ` `} ` ` `  `// This code is contributed ` `// by Sam007 `

## PHP

 ` `

Output:

```Original Array
1 2 3 4 5 6 7 8 9
Modified Array
9 1 8 2 7 3 6 4 5 ```

Time Complexity : O(n)
Auxiliary Space : O(n)

Exercise : How to solve this problem if extra space is not allowed?

Rearrange an array in maximum minimum form | Set 2 (O(1) extra space)

## tags:

Arrays Sorting Zoho Zoho Arrays Sorting