Rearrange an Array in Maximum Minimum Form using C++

C++Server Side ProgrammingProgramming

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.

raja
Updated on 26-Nov-2021 10:26:52

Advertisements