Rearrange array such that even index elements are smaller and odd index elements are greater in C++

C++Server Side ProgrammingProgramming

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 less 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 index elements are smaller and odd index elements are greater is: 1 4 2 6 3 8 5 7

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 less than the elements at an odd position and the array formed after performing this operation is 1 4 2 6 3 8 5 7.

Input − int arr[] = {10, -1, 7, -5, 6, -9}

Output − Array before Arrangement: 10 -1 7 -5 6 -9 Rearrangement of an array such that even index elements are smaller and odd index elements are greater is: -1 10 -5 7 -9 6

Explanation − we are given an integer array of size 6 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 less than the elements at an odd position and the array formed after performing this operation is -1 10 -5 7 -9 6 .

Approach used in the below program is as follows

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

  • Print an array before performing the rearrangement action using the FOR loop.

  • Call to the function Rearrangement(arr, size) by passing array and size of an array as a parameter.

  • Inside the function Rearrangement(arr, size)

    • Start loop FOR from i to 0 till i less than size -1. Inside the loop, check IF i % 2 to 0 then check IF arr[i] greater than arr[i + 1] then call the swap method of C++ STL by passing arr[i] and arr[i + 1] to the sort method.

    • Now, check IF i % 2 not equals to 0 then check IF arr[i] less than arr[i + 1] then call the swap method of STL by passing arr[i] and arr[i + 1] to the method.

  • Print the result.

Example

#include <iostream>
using namespace std;
void Rearrangement(int* arr, int size){
   for(int i = 0; i < size - 1; i++){
      if(i % 2 == 0 ){
         if(arr[i] > arr[i + 1]){
            swap(arr[i], arr[i + 1]);
         }
      }
      if(i % 2 != 0){
         if(arr[i] < arr[i + 1]){
            swap(arr[i], arr[i + 1]);
         }
      }
   }
}
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] << " ";
   }
   //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 index elements are smaller and odd index elements are greater 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 index elements are smaller and odd index elements are greater is: 1 4 2 6 3 8 5 7
raja
Published on 02-Nov-2021 06:40:49
Advertisements