Home  >  Article  >  Backend Development  >  C++ program to rearrange array in max-min form

C++ program to rearrange array in max-min form

王林
王林forward
2023-08-28 14:29:06767browse

C++ program to rearrange array in max-min form

We get an array of integers, which can be arranged in a sorted/unsorted manner. The task is to first sort the array (if the values ​​are not sorted) and then arrange the array in such a way that the first element of the array is the maximum value, the second element is the minimum value, and the third element is the array. The second maximum, the fourth will be the second minimum, and so on.

Let’s look at various input and output scenarios for this situation -

Input − int arr[] = {7, 5, 2, 3, 4, 9, 10, 5 }

Output− Array before sorting: 2 3 4 5 5 7 9 10 Rearrange the array in max-min form as: 10 2 9 3 7 4 5 5

Explanation− We get an array of integer type containing the values ​​{7, 5, 2, 3, 4, 9, 10, 5}. First, we sort an array, it will be {2 3 4 5 5 7 9 10}. Second, arrange the largest element at arr[0] (i.e. 10), then arrange the smallest element at arr[1] (i.e. 2), then arrange the second largest element at arr[2] (i.e. 9), and so on analogy. The final result array will be 10 2 9 3 7 4 5 5

Input− int arr[] = {2, 4, 1, 6, 7}

Output− Array before sorting: 1, 2, 4, 6, 7 Rearrange the array in max-min form as: 7, 1, 6, 2, 4

Explanation− We get an array of integer type containing the values ​​{2, 4, 1, 6 , 7}. First we sort an array, the result is {1,2,4,6,7}. Secondly, arrange the largest element at arr[0], that is 7 then the smallest element at arr[1] which is 1 then the second largest element at arr[2] which is 6 and so on. The final array will be 7, 1, 6, 2, 4

The method used in the following program is as follows

  • Input an array of integer type elements and calculate the value of the array size. Call the C STL's sort method by passing arr[] and the array size as arguments to the function.

  • Print the array before sorting and call the function Rearr_Max_Min(arr, size)

  • Inside the function Rearr_Max_Min(arr, size)

    • Declare one variable as max and set it to size - 1, and set another variable as min and set it to 0. Declare the variable as max_val and set it to arr[size - 1] 1.

    • Start looping FOR from i to 0 until i is 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 maximum value by 1.

    • Otherwise, set arr[i] to arr[i] (arr[min] % max_val) * max_val and increase min by 1.

    • Start looping FOR from i to 0 until i is 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

The above is the detailed content of C++ program to rearrange array in max-min form. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete