
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Rearrange an array such that every odd indexed element is greater than its previous in C++
We are given a positive integer type array, let's say, arr[] of any given size. The task is to rearrange an array in such a manner that all the elements present at an odd index should value greater than the element presents at an even index and print the result.
Let us see various input output scenarios for this −
Input − int arr[] = {2, 1, 5, 4, 3, 7, 8}
Output − Array before Arrangement: 2 1 5 4 3 7 8 Rearrangement of an array such that every odd indexed element is greater than it previous is: 1 4 2 5 3 8 7
Explanation − we are given an integer array of size 7. Now, we will swap the elements at even index with the elements at odd index if even indexed elements are greater i.e.
Arr[0] > arr[1] = call swap = {1, 2, 5, 4, 3, 7, 8} Arr[2] > arr[3] = call swap = {1, 2, 4, 5, 3, 7, 8} Arr[6] > arr[5] = call swap = {1, 2, 4, 5, 3, 8, 7} Arr[2] > arr[1] = call swap = {1, 4, 2, 5, 3, 8, 7}
Input − int arr[] = {3, 2, 6, 9}
Output − Array before Arrangement: 3 2 6 9 Rearrangement of an array such that every odd indexed element is greater than it previous is: 2 3 6 9
Explanation − we are given an integer array of size 4. Now, we will swap the elements at even index with the elements at odd index if even indexed elements are greater i.e. Arr[0] > arr[1] = call swap = {2, 3, 6, 9}. No need to further call the swap method as all the elements at positions satisfies the conditions
Approach used in the below program is as follows
Input an array of integer type elements and calculate the size of an array.
Print the array before arrangement and call the function Rearrangement(arr, size)
Inside the function Rearrangement(arr, size)
Create a variable of integer type let’s say, ptr and set it with size-1.
Start loop FOR, from i to 0 till i less than ptr and i = i + 1. Inside the loop, check if arr[i] is greater than arr[i+1] then call swap(arr[i], arr[i+1]).
Check IF size & 1 then start loop FOR from i to ptr till i greater than 0 and i = i - 2. Inside the loop, check IF arr[i] greater than arr[i - 1] then call swap(arr[i], arr[i-1])
Print the array after the rearrangement of values of an array.
Example
#include <iostream> using namespace std; void Rearrangement(int arr[], int size){ int ptr = size - 1; for(int i = 0; i < ptr; i = i+2){ if(arr[i] > arr[i+1]){ swap(arr[i], arr[i+1]); } } if(size & 1){ for(int i = ptr; i > 0; i = i-2){ if(arr[i] > arr[i-1]){ swap(arr[i], arr[i-1]); } } } } int main(){ //input an array int arr[] = {2, 1, 5, 4, 3, 7, 8}; 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 every odd indexed element is greater than it previous 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 5 4 3 7 8 Rearrangement of an array such that every odd indexed element is greater than it previous is: 1 4 2 5 3 8 7
- Related Articles
- Rearrange array such that even positioned are greater than odd in C++
- Elements greater than the previous and next element in an Array in C++
- Rearrange array such that even index elements are smaller and odd index elements are greater in C++
- Rearrange an array such that arr[i] = i in C++
- Previous greater element in C++
- Find a string such that every character is lexicographically greater than its immediate next character in Python
- Python – Sort Matrix by Number of elements greater than its previous element
- Adding elements of an array until every element becomes greater than or equal to k in C++.
- Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ in C++
- C++ program to Adding elements of an array until every element becomes greater than or equal to k
- Absolute Difference of even and odd indexed elements in an Array (C++)?
- Absolute Difference of even and odd indexed elements in an Array in C++?
- Find closest greater value for every element in array in C++
- Finding ‘k’ such that its modulus with each array element is same in C++
- Find Array Elements Which are Greater Than Its Immediate Left Element?
