Home > Article > Backend Development > Rearrange odd and even values in ascending alternating order in C++
We get an array of integer type containing positive and negative numbers, say, arr[] of any given size. The task is to rearrange the array in such a way that when the lowest element in the array is odd, the elements in the array will be rearranged in such a way that odd numbers are first and even numbers are first The second way. When the lowest element in the array is even, the elements of the array will be rearranged in a manner of even first, odd second, if the number of even/odd elements is greater than the number of odd/even elements, then it will place 0 and print the result .
Input− int arr[] = { 1, 1, 2, 2, 5, 4 }
Output− The odd and even values in ascending order in an alternating manner are: 1 2 1 2 5 4.
Explanation- We get an array of integer type. Now we will check the smallest element in the array i.e. 1 is an odd number so the elements will be arranged in the odd number first, Even the second way i.e. 1 2 1 2 5 4 is the final output.
Input− int arr[] = { 6, 3, 2, 8, 10, 4 }
Output− Press in an alternating manner Rearranging the odd and even values in ascending order is: 2 3 4 0 6 0
Explanation − We get an array of integer type. Now we will check the smallest element in the array i.e. 2 is an even number so elem
Enter an array of integer elements and Calculate the size of the array.
To sort an array using the C STL's sort method, pass the array and size of the array to the sort function.
Declare an integer variable and set it by calling the function Rearrangement(arr, size)
In the function Rearrangement(arr, size) Inside
Create two variables "vec_1" and "vec_2" as stored vector type integer type data.
Create a temporary variable temp of integer type and set it to 0.
Declare another variable of type bool as a check and set it to FALSE.
Start a FOR loop from i to 0 until i is less than size. Inside the loop, check IF arr[i] % 2 = 0, then push arr[i] into vec_1. Otherwise, push arr[i] into vec_2.
Declare integer variables as i and j as 0. Check IF arr[0] % 2 = 0, then set the check to true.
Start when the temperature is less than the size. Inside the loop, check IF check = true, then set arr[temp ] to vec_1[i ] and check to !check. Otherwise, convert arr[temp ] to vec_2[j ] and set check to !check.
Print the result.
li>#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; }
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
The above is the detailed content of Rearrange odd and even values in ascending alternating order in C++. For more information, please follow other related articles on the PHP Chinese website!