-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlternative Sorting.cpp
57 lines (49 loc) · 1.16 KB
/
Alternative Sorting.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
54
55
56
57
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
vector<int> alternateSort(vector<int>& arr) {
// Your code goes here
vector<int>temp = arr;
sort(temp.begin(),temp.end());
vector<int>ans;
int i = 0 , j =temp.size()-1;
while( i<=j){
if(i==j){
ans.push_back(temp[i]);
i++;j--;
}else {
ans.push_back(temp[j--]);
ans.push_back(temp[i++]);
}
}
return ans;
}
};
//{ Driver Code Starts.
int main() {
string ts;
getline(cin, ts);
int t = stoi(ts);
while (t--) {
vector<int> arr;
string input;
getline(cin, input);
stringstream ss(input);
int number;
while (ss >> number) {
arr.push_back(number);
}
Solution obj;
vector<int> ans = obj.alternateSort(arr);
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
cout << endl;
cout << "~" << endl;
}
return 0;
}
// } Driver Code Ends