C++ program to rearrange an array in maximum minimum form

C++Server Side ProgrammingProgramming

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
raja
Updated on 02-Nov-2021 06:11:54

Advertisements