- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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

- Related Questions & Answers
- Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j] in C++
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) in C++
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i] in C++
- Maximize arr[j] – arr[i] + arr[l] – arr[k], such that i < j < k < l in C++
- Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ in C++
- Count of unique pairs (arr[i], arr[j]) such that i < j in C++
- Rearrange an array such that arr[i] = i in C++
- Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space using C++
- Count pairs in an array that hold i*arr[i] > j*arr[j] in C++
- Find Maximum value of abs(i – j) * min(arr[i], arr[j]) in an array arr[] in C++
- Maximum value of |arr[i] – arr[j] - + |i – j| in C++
- Maximize value of (arr[i] – i) – (arr[j] – j) in an array in C++
- Maximum value of arr[i] % arr[j] for a given array in C++
- Rearrange an Array to Maximize i*arr[i] using C++
- Maximum modulo of all the pairs of array where arr[i] >= arr[j] in C++