-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathLargestNumber.cpp
53 lines (47 loc) · 1.29 KB
/
LargestNumber.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
//Code by Arunika Bhattacharya
//Largest Number Problem
/*Given a list of non negative integers, arrange them in such a manner that they form the largest number possible.The result is going to be very large, hence return the result in the form of a string.
*/
#include<bits/stdc++.h>
using namespace std;
string largestNumber(vector<int>& nums) {
int x =0 ;
//Checking for zeroes in the array
for(int i=0 ;i<nums.size();i++){
if(nums[i]==0){
x++ ;
}
}
if(x == nums.size()){
return "0";
}
//converting integer vector to string vector
vector<string> s(nums.size());
for(int i =0 ;i<nums.size();i++){
s[i] = to_string(nums[i]);
}
//sorting the vector
sort(s.begin(),s.end(),[](string X,string Y){
string XY = X.append(Y);
string YX = Y.append(X);
return XY.compare(YX)>0 ? 1 : 0;
});
//empty string
string ans="" ;
for(int i=0 ;i<nums.size();i++){
ans+=s[i];
}
return ans;
}
int main(){
int n ;
cin>>n;
//InputVector
vector<int> arr(n);
for(int i=0 ;i<n;i++){
cin>>arr[i];
}
auto res =largestNumber(arr);
cout<<res<<"\n";
return 0 ;
}