- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 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
- Related Articles
- Alternate Odd and Even Nodes in a Singly Linked List in C++
- Rearrange array such that even positioned are greater than odd in C++
- MySQL command to order timestamp values in ascending order?
- Rearrange array such that even index elements are smaller and odd index elements are greater in C++
- Even numbers at even index and odd numbers at odd index in C++
- Matching odd even indices with values in JavaScript
- MySQL Order by a specific column x and display remaining values in ascending order
- Odd Even Jump in C++
- Positive elements at even and negative at odd positions (Relative order not maintained) in C++
- MySQL command to order timestamp values in ascending order without using TIMESTAMP()?
- Check if it is possible to rearrange rectangles in a non-ascending order of breadths in Python
- Program to sort all even and odd numbers in increasing and decreasing order respectively in Python
- Count even and odd digits in an Integer in C++
- Largest Even and Odd N-digit numbers in C++
- Separate odd and even in JavaScript
