forked from 7harshit20/dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
largest_num_from_arr.cpp
65 lines (64 loc) · 1.33 KB
/
largest_num_from_arr.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
58
59
60
61
62
63
64
65
#include<iostream>
#include<vector>
#include<string>
using namespace std;
bool islarge(string a,string b){
return stoi(a+b)>stoi(b+a);
}
void merge(vector<string> &arr,int l,int mid,int r){
vector<string> tarr(r-l+1);
int tl=0,tmid=mid-l,tr=r-l;
for(int i=0;i<r-l+1;i++){
tarr[i]=arr[i+l];
}
for(int i=0;i<r-l+1;i++){
if(tmid+1>r-l){
arr[i+l]=tarr[tl];
tl++;
continue;
}
if(tl>mid-l){
arr[i+l]=tarr[tmid+1];
tmid++;
continue;
}
if( islarge(tarr[tl],tarr[tmid+1]) ){
arr[i+l]=tarr[tmid+1];
tmid++;
}
else{
arr[i+l]=tarr[tl];
tl++;
}
}
}
void mergesort(vector<string> &arr,int l,int r){
if(l<r){
int mid=(l+r)/2;
mergesort(arr,l,mid);
mergesort(arr,mid+1,r);
merge(arr,l,mid,r);
}
}
string printLargest(vector<string> &arr){
mergesort(arr,0,arr.size()-1);
string ans;
for(int i=arr.size()-1;i>=0;i--){
ans+=arr[i];
}
return ans;
}
int main(){
int n;
cin>>n;
vector<string> v(n);
for(auto &i:v){
cin>>i;
}
// for(auto ele:v)
// cout<<ele<<" ";
cout<<endl;
// cout<<islarge("30","3")<<endl;
cout<<printLargest(v);
return 0;
}