Rearrange array such that even positioned are greater than odd in C++


We are given an integer type array containing both positive and negative numbers, let's say, arr[] of any given size. The task is to rearrange an array in such a manner that all the elements at an even position or index should be greater than the elements at an odd position or index and print the result.

Let us see various input output scenarios for this −

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

Output − Array before Arrangement: 2 1 4 3 6 5 8 7 Rearrangement of an array such that even positioned are greater than odd is: 1 2 3 4 5 6 7 8

Explanation − we are given an integer array of size 8 containing both positive and negative elements. Now, we will rearrange the array in such a manner that all the elements at an even position are greater than the elements at an odd position and the array formed after performing this operation is 1 2 3 4 5 6 7 8.

Input − int arr[] = {-3, 2, -4, -1}

Output − Array before Arrangement: -3 2 -4 -1 Rearrangement of an array such that even positioned are greater than odd is: -4 -3 -1 2

Explanation −we are given an integer array of size 8 containing both positive and negative elements. Now, we will rearrange the array in such a manner that all the elements at an even position are greater than the elements at an odd position and the array formed after performing this operation is -4 -3 -1 2.

Approach used in the below program is as follows

  • Input an array of integer type elements and calculate the size of an array.

  • Sort an array using the sort method of C++ STL by passing array and size of an array to the sort function.

  • Declare an integer variable and set it with the call to the function Rearrangement(arr, size)

  • Inside the function Rearrangement(arr, size)

    • Declare an integer type array let’s say, ptr[size] of same size of array arr[size]

    • Declare temporary integer type variables i.e. first to 0 and last to size -1.

    • Start loop FOR from i to 0 till i is less than the size of an array. Inside the loop, check IF (i + 1) % 2 equals to 0 then set ptr[i] to arr[last--].

    • ELSE, set ptr[i] to arr[first++].

  • Print the result.

Example

#include <bits/stdc++.h>
using namespace std;
void Rearrangement(int* arr, int size){
   int ptr[size];
   int first = 0;
   int last = size - 1;
   for (int i = 0; i < size; i++){
      if((i + 1) % 2 == 0){
         ptr[i] = arr[last--];
      }
      else{
         ptr[i] = arr[first++];
      }
   }
}
int main(){
   //input an array
   int arr[] = {2, 1, 4, 3, 6, 5, 8, 7};
   int size = sizeof(arr) / sizeof(arr[0]);
   //print the original Array
   cout<<"Array before Arrangement: ";
   for (int i = 0; i < size; i++){
      cout << arr[i] << " ";
   }
   //sort an Array
   sort(arr, arr + size);
   //calling the function to rearrange the array
   Rearrangement(arr, size);
   //print the array after rearranging the values
   cout<<"\nRearrangement of an array such that even positioned are greater than odd 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 1 4 3 6 5 8 7
Rearrangement of an array such that even positioned are greater than odd is: 1 2 3 4 5 6 7 8

Updated on: 02-Nov-2021

165 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements