Question #
w
Awesome sort
You are an array A of size M
Task
You have to sort the array such that the following conditions hold true
All even numbers must come before all odd numbers
2. All even numbers that are divisible by 5 must come first than even numbers not divisible by 5.
3 hve even numbers are divisible by 5 then the number having a greater value will come first.
4 even numbers are not divisible by 5 then the number having a greater index in the array will come first.
5 A# odd numbers must come in relative order as they are present in the array,
Example
Assum
• N=4
• A=[5,10,30,7]
Aproch
• Even numbers = [10 ,30]
• Odd numbers =[5,7]
• After sorting
Answers
Answer: - The array can be sorted by using sorting feature. You can sort the array by following the given program below.
Program: -
#include <bits/stdc++.h>
using namespace std;
// Function to sort array in the way
// mentioned above
void AwesomeSort(vector<int> m, int n)
{
// Create three vectors
vector<int> v1, v2, v3;
int i;
// Traverse through the elements
// of the array
for (i = 0; i < n; i++) {
// If elements are even and
// divisible by 10
if (m[i] % 10 == 0)
v1.push_back(m[i]);
// If number is even but not divisible
// by 5
else if (m[i] % 2 == 0)
v2.push_back(m[i]);
else
// If number is odd
v3.push_back(m[i]);
}
// Sort v1 in descending order
sort(v1.begin(), v1.end(), greater<int>());
for (int i = 0; i < v1.size(); i++) {
cout << v1[i] << " ";
}
for (int i = v2.size()-1; i >= 0; i--) {
cout << v2[i] << " ";
}
for (int i = 0; i < v3.size(); i++) {
cout << v3[i] << " ";
}
}
// Driver Code
int main()
{
// Given Input
vector<int> arr{ 5, 10, 30, 7 };
int N = arr.size();
// FunctionCall
AwesomeSort(arr, N);
return 0;
}
- First you need to create three vectors and then arrange them in a range. After that you need to sort the vector and print the vector in descending order.
To know more about the topic, visit the below links: -
https://brainly.in/question/16268939
https://brainly.in/question/22631510
#SPJ1