# Rearrange Odd and Even values in Alternate Fashion in Ascending Order 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 when the lowest element in an array is odd then elements of an array will be rearranged in odd first and even second manner. When the lowest element in an array is even then elements of an array will be rearranged in even first and odd second manner and if the number of even/odd elements is more than the number of odd/even elements then it will place the 0’s and print the result.

## Let us see various input output scenarios for this −

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

Output − Rearrangement of Odd and Even values in Alternate Fashion in Ascending Order is: 1 2 1 2 5 4.

Explanation − We are given an integer type array. Now we will check which is the smallest element in an array i.e. 1 which is the odd value so elements will be arranged in odd first and even second manner i.e. 1 2 1 2 5 4 is the final output.

Input − int arr[] = { 6, 3, 2, 8, 10, 4 }

Output − Rearrangement of Odd and Even values in Alternate Fashion in Ascending Order is: 2 3 4 0 6 0

Explanation − We are given an integer type array. Now we will check which is the smallest element in an array i.e. 2 which is the even value so elem

## 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)

• Create two variables as ‘vec_1’ and ‘vec_2’ as a type of vector that is storing integer type data.

• Create a temporary variable of type integer as temp and set it with 0.

• Declare another variable of type bool as check and set it with value FALSE.

• Start loop FOR from i to 0 till i less than size. Inside the loop, check IF arr[i] % 2 = 0 then push arr[i] to vec_1. ELSE, push arr[i] to vec_2.

• Declare integer variables as i and j to 0. Check IF arr[0] % 2 = 0 then set check to true.

• Start WHILE temp less than size. Inside the loop, check IF check = true then set arr[temp++] to vec_1[i++] and set check to !check. ELSE, arr[temp++] to vec_2[j++] and set check to !check.

• Print the result.

## Example

#include <bits/stdc++.h>
using namespace std;
void Rearrangement(int arr[], int size){
vector<int> vec_1, vec_2;
int temp = 0;
bool check = false;
for(int i = 0; i < size; i++){
if(arr[i] % 2 == 0){
vec_1.push_back(arr[i]);
}
else{
vec_2.push_back(arr[i]);
}
}
int i = 0;
int j = 0;
if(arr[0] % 2 == 0){
check = true;
}
while(temp < size){
if(check == true){
arr[temp++] = vec_1[i++];
check = !check;
}
else{
arr[temp++] = vec_2[j++];
check = !check;
}
}
}
int main(){
int arr[] = { 1, 1, 2, 2, 5, 4 };
int size = sizeof(arr) / sizeof(int);
//sort an array
sort(arr, arr + size);
cout<<"Rearrangement of Odd and Even values in Alternate Fashion in Ascending Order is: ";
Rearrangement(arr, size);
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

Rearrangement of Odd and Even values in Alternate Fashion in Ascending Order is: 1 2 1 2 5 4

Updated on: 02-Nov-2021

1K+ Views