-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEven Swap.cpp
53 lines (46 loc) · 976 Bytes
/
Even Swap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//{ Driver Code Starts
// Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function Template for C++
class Solution
{
public:
vector<int> lexicographicallyLargest(vector<int> &a, int n)
{
int i = 0;
while (i < n)
{
int j = i + 1;
while (j < n && (a[j] + a[j - 1]) % 2 == 0)
{
j++;
}
sort(a.begin() + i, a.begin() + j, greater<int>()); // Sorts in Descending order
i = j;
}
return a;
}
};
//{ Driver Code Starts.
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector<int> a(n);
for (auto &j : a)
cin >> j;
Solution s;
vector<int> b = s.lexicographicallyLargest(a, n);
for (auto i : b)
cout << i << " ";
cout << endl;
}
return 0;
}
// } Driver Code Ends