Home >Backend Development >C++ >Rearrange array order using C++ - minimum value, maximum value, second minimum value, second maximum value

Rearrange array order using C++ - minimum value, maximum value, second minimum value, second maximum value

王林
王林forward
2023-09-11 22:29:02866browse

使用C++重新排列数组顺序 - 最小值、最大值、第二小值、第二大值

We get an array; we need to arrange this array in the following order: the first element should be the smallest element, the second element should be the largest element, and the third element should be The second smallest element, the fourth element should be the second largest element and so on 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 }

Ways to find the solution

You can use two variables " x" and "y" to resolve the positions they point to to the largest and smallest elements, but for this array should be sorted, so we need to sort the array first, and then create an array of the same size A new empty array to store the reordered array. Now iterate over the array and if the iterated element is at an even index, add arr[ x ] element to the empty array and increment x by 1. If the element is at an odd index, add the 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

The above code description

  • Variables are initialized to x=0 and y = array_length(n) - 1.
  • while( x<=y) Iterates through the array until x is greater than y.
  • If the count is even (x), the element is added to the final array and the variable x is incremented by 1.
  • If i is an odd number, (y) the element is added to the final array, and the variable y is decremented by 1.
  • Finally, store the reordered array in reordered_array[].

Conclusion

In this article, we discussed the solution to rearrange the given array in minimum and maximum form. We also wrote a C program for this. Likewise, we can write this program in any other language like C, Java, Python, etc. We hope this article was helpful to you.

The above is the detailed content of Rearrange array order using C++ - minimum value, maximum value, second minimum value, second maximum value. 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