Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i in C++


We are given an integer array containing odd and even integer values. The task is to rearrange an array in such a manner that arr[i] should be greater than or equals to arr[j] based on the condition that value at index arr[i] should be even and if value at arr[i] is odd then arr[i] should be less than equals to arr[j].

Let us see various input output scenarios for this −

Input − int arr[] = {5, 9, 10, 12, 32, 35, 67, 89}

Output − Array after rearranging elements are: 12 32 10 35 9 67 5 89

Explanation − we are given an array with the odd and even integers. Now, we will start traversing from the arr[i] position against arr[j] position and check IF arr[i] is even then make sure that arr[i] should be greater than arr[j] and IF arr[i] is odd then make sure that arr[i] should be less than equals to arr[j].

Input − int arr[] = {4, 5, 1, 2, 9, 10}

Output − Array after rearranging elements are: 4 5 2 9 1 10

Explanation − we are given an array with the odd and even integers. Now, we will start traversing from the arr[i] position against arr[j] position and check IF arr[i] is even then make sure that arr[i] should be greater than arr[j] and IF arr[i] is odd then make sure that arr[i] should be less than equals to arr[j].

Approach used in the below program is as follows

  • Declare an array of integer types. Calculate the size of an array as size = sizeof(arr) / sizeof(arr[0]).

  • Call a function as array_rearrange(arr, size) and pass data as parameter.

    • Declare a variable as even and set it to even = size / 2 and declare another variable as odd and set it to size - even.

    • Declare a variable as temp and set it to odd - 1. Declare an array_2[] with size of arr_1[].

    • Start loop FOR i to 0 and i less than size. Inside the loop, set it to arr_2[i] to arr[i].

    • Call a function as sort(arr_2, arr_2 + size).

    • Start loop FOR from i to 0 till i less than size. Inside the loop, set arr[i] to arr_2[temp] and decrement the variable temp by 1.

    • Set temp to odd. Start loop FOR from i to 1 till i less than size. Inside the loop, set arr[i] to arr_2[temp] and increment the temp by 1.

    • Start loop FOR from i to 0 till i less than size. Print the arr[i].

Example

#include <bits/stdc++.h>

using namespace std;
void array_rearrange(int arr[], int size){
   int even = size / 2;
   int odd = size - even;
   int temp = odd - 1;
   int arr_2[size];
   for(int i = 0; i < size; i++){
      arr_2[i] = arr[i];
   }
   sort(arr_2, arr_2 + size);
   for(int i = 0; i < size; i += 2){
      arr[i] = arr_2[temp];
      temp--;
   }
   temp = odd;
   for(int i = 1; i < size; i += 2){
      arr[i] = arr_2[temp];
      temp++;
   }
   cout<<"Array after rearranging elements are: ";
   for (int i = 0; i < size; i++){
      cout << arr[i] << " ";
   }
}
int main(){
   int arr[] = {5, 9, 10, 12, 32, 35, 67, 89};
   int size = sizeof(arr) / sizeof(arr[0]);
   array_rearrange(arr, size);
   return 0;
}

Output

If we run the above code it will generate the following Output

Array after rearranging elements are: 12 32 10 35 9 67 5 89

Updated on: 02-Nov-2021

128 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements