Skip to content

Instantly share code, notes, and snippets.

@Ch-sriram
Created June 4, 2020 06:22
Show Gist options
  • Select an option

  • Save Ch-sriram/a05ab29757205a509f22a1f28c030d75 to your computer and use it in GitHub Desktop.

Select an option

Save Ch-sriram/a05ab29757205a509f22a1f28c030d75 to your computer and use it in GitHub Desktop.
Largest Concatenated Number
// Problem Link: https://practice.geeksforgeeks.org/problems/largest-number-formed-from-an-array/0
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
#define ull uint64_t
void largestConcatenatedNumber(vector<string> &nums) {
sort(nums.begin(), nums.end(),
[](string X, string Y){
string XY = X.append(Y);
string YX = Y.append(X);
return (XY.compare(YX) < 0 ? 0 : 1);
});
for(ull i = 0; i < nums.size(); ++i)
cout << nums[i];
cout << endl;
}
int main() {
int t; cin >> t;
while(t--) {
int n; cin >> n;
vector<string> nums(n);
for(int i = 0; i < n; ++i)
cin >> nums[i];
largestConcatenatedNumber(nums);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment