-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add solution of 25 october issue #47
- Loading branch information
1 parent
55c03be
commit 243a1fb
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//{ 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>v; | ||
for(int i=0;i<arr.size();i++){ | ||
v.push_back(arr[i]); | ||
} | ||
sort(v.begin(),v.end()); | ||
int first=0; | ||
int second =v.size()-1; | ||
int count=0; | ||
while(first<second){ | ||
arr[count++]=v[second]; | ||
arr[count++]=v[first]; | ||
first++; | ||
second--; | ||
} | ||
if(first ==second){ | ||
arr[count]=v[first]; | ||
} | ||
return arr; | ||
|
||
} | ||
}; | ||
|
||
|
||
//{ 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 |