- 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

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.

**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 .

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.

#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; }

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

- Related Questions & Answers
- Rearrange array such that even positioned are greater than odd in C++
- Swap Even Index Elements And Odd Index Elements in Python
- Even numbers at even index and odd numbers at odd index in C++
- Find elements of an Array which are Odd and Even using STL in C++
- Rearrange an array such that every odd indexed element is greater than its previous in C++
- Odd even index difference - JavaScript
- Count elements such that there are exactly X elements with values greater than or equal to X in C++
- Python – Check if elements index are equal for list elements
- Maximum sum in circular array such that no two elements are adjacent in C++
- Maximum sum such that no two elements are adjacent in C++
- Find an array element such that all elements are divisible by it using c++
- Count number of even and odd elements in an array in C++
- Swapping even and odd index pairs internally in JavaScript
- Absolute Difference of even and odd indexed elements in an Array (C++)?
- Maximum value K such that array has at-least K elements that are >= K in C++

Advertisements