Rearrange An Array In Order – Smallest, Largest, 2nd Smallest, 2nd Largest,. Using C++

C++Server Side ProgrammingProgramming

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.

raja
Published on 26-Nov-2021 10:29:43
Advertisements