
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Rearrange An Array In Order – Smallest, Largest, 2nd Smallest, 2nd Largest,. Using C++
We are given an array; we need to arrange this array in an order that the first element should be a minimum element, the second element should be a maximum element, the third element should be 2nd minimum element, the fourth element should be the 2nd maximum element and so on for example −
Input : arr[ ] = { 13, 34, 30, 56, 78, 3 } Output : { 3, 78, 13, 56, 34, 30 } Explanation : array is rearranged in the order { 1st min, 1st max, 2nd min, 2nd max, 3rd min, 3rd max } Input : arr [ ] = { 2, 4, 6, 8, 11, 13, 15 } Output : { 2, 15, 4, 13, 6, 11, 8 }
Approach to find The Solution
This problem can be solved using two variables, 'x's and 'y' where they will point to the maximum and minimum element, But for that array should be sorted, so we need to sort the array first, Then create a new empty array of the same size to store the reordered array. Now iterate over the array and if the iterative element is at even index, then add arr[ x ] element to the empty array and increment x by 1. if the element is at an odd index, then add arr[ y ] element to the empty array and decrement y by 1. Do this until y becomes smaller than x.
Example
#include <bits/stdc++.h> using namespace std; int main () { int arr[] = { 2, 4, 6, 8, 11, 13, 15 }; int n = sizeof (arr) / sizeof (arr[0]); // creating a new array to store the rearranged array. int reordered_array[n]; // sorting the original array sort(arr, arr + n); // pointing variables to minimum and maximum element index. int x = 0, y = n - 1; int i = 0; // iterating over the array until max is less than or equals to max. while (x <= y) { // if i is even then store max index element if (i % 2 == 0) { reordered_array[i] = arr[x]; x++; } // store min index element else { reordered_array[i] = arr[y]; y--; } i++; } // printing the reordered array. for (int i = 0; i < n; i++) cout << reordered_array[i] << " "; // or we can update the original array // for (int i = 0; i < n; i++) // arr[i] = reordered_array[i]; return 0; }
Output
2 15 4 13 6 11 8
Explanation of the above code
- Variables are initialised as x=0 and y = array_length(n) - 1.
- while( x<=y) traverse the array until x becomes greater than y.
- If the count is even (x), the element is added to the final array, and variable x is incremented by 1.
- If i is odd, then (y)the element is added in the final array, and variable y is decremented by 1.
- Finally, the Reordered array is stored in the reordered_array[ ].
Conclusion
In this article, we discussed the solution to rearrange the given array in the smallest, largest form. 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.
- Related Articles
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- Java program to find the 2nd smallest number in an array
- Java program to find the 2nd largest number in an array
- Find the 2nd smallest number in a Java array.
- Find the 2nd largest number in a Java array.
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- K’th Smallest/Largest Element using STL in C++
- K’th Smallest/Largest Element in Unsorted Array in C++
- C program to find the second largest and smallest numbers in an array
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- kth smallest/largest in a small range unsorted array in C++
- Difference between the largest and the smallest primes in an array in Java
- Finding the largest and smallest number in an unsorted array of integers in JavaScript
- Largest and smallest word in a string - JavaScript
- Find smallest and largest elements in singly linked list in C++
