- 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 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<<"

Rearrangement 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

- Related Questions & Answers
- Rearrange array such that even index elements are smaller and odd index elements are greater in C++
- Rearrange an array such that every odd indexed element is greater than its previous in C++
- Rearrange an array such that arr[i] = i in C++
- Rearrange characters in a string such that no two adjacent are same in C++
- 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++
- Count elements such that there are exactly X elements with values greater than or equal to X in C++
- Rearrange Odd and Even values in Alternate Fashion in Ascending Order in C+=+
- Largest even digit number not greater than N in C++
- How to find numbers in an array that are greater than, less than, or equal to a value in java?
- Finding all the n digit numbers that have sum of even and odd positioned digits divisible by given numbers - JavaScript
- Odd even sort in an array - JavaScript
- Returning array values that are not odd in JavaScript
- Find a string such that every character is lexicographically greater than its immediate next character in Python
- Count natural numbers whose all permutation are greater than that number in C++
- Delete all the nodes from the list that are greater than x in C++