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
#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;
}
Copy after login
Output If we run the above code it will generate the following output #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; }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

This article details C function return types, encompassing basic (int, float, char, etc.), derived (arrays, pointers, structs), and void types. The compiler determines the return type via the function declaration and the return statement, enforcing

Gulc is a high-performance C library prioritizing minimal overhead, aggressive inlining, and compiler optimization. Ideal for performance-critical applications like high-frequency trading and embedded systems, its design emphasizes simplicity, modul

This article explains C function declaration vs. definition, argument passing (by value and by pointer), return values, and common pitfalls like memory leaks and type mismatches. It emphasizes the importance of declarations for modularity and provi

This article details C functions for string case conversion. It explains using toupper() and tolower() from ctype.h, iterating through strings, and handling null terminators. Common pitfalls like forgetting ctype.h and modifying string literals are

This article examines C function return value storage. Small return values are typically stored in registers for speed; larger values may use pointers to memory (stack or heap), impacting lifetime and requiring manual memory management. Directly acc

This article analyzes the multifaceted uses of the adjective "distinct," exploring its grammatical functions, common phrases (e.g., "distinct from," "distinctly different"), and nuanced application in formal vs. informal

This article details efficient STL algorithm usage in C . It emphasizes data structure choice (vectors vs. lists), algorithm complexity analysis (e.g., std::sort vs. std::partial_sort), iterator usage, and parallel execution. Common pitfalls like

This article explains the C Standard Template Library (STL), focusing on its core components: containers, iterators, algorithms, and functors. It details how these interact to enable generic programming, improving code efficiency and readability t
