Created
March 7, 2020 21:40
-
-
Save SuryaPratapK/e7211a71677eb30933ae9c22fda6bb55 to your computer and use it in GitHub Desktop.
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
//#include <iostream> | |
#include<bits/stdc++.h> | |
using namespace std; | |
bool comparator(string first,string second) | |
{ | |
string one = first+second; | |
string two = second+first; | |
int i=0; | |
while(one[i] && two[i]) | |
{ | |
if(one[i]>two[i]) | |
return true; | |
else if(one[i]<two[i]) | |
return false; | |
++i; | |
} | |
return false; | |
} | |
int main() { | |
//code | |
int tc; | |
cin>>tc; | |
while(tc--) | |
{ | |
int len; | |
vector<string> arr; | |
//Take Array inputs | |
int i; | |
string temp; | |
cin>>len; | |
for(i=0;i<len;++i) | |
{ | |
cin>>temp; | |
arr.push_back(temp); | |
} | |
sort(arr.begin(),arr.end(),comparator); | |
for(i=0;i<len;++i) | |
cout<<arr[i]<<" "; | |
cout<<"\n"; | |
} | |
return 0; | |
} |
public class HighNumber {
public static void main(String[] args) {
int[] arr = { 3, 30, 8, 5, 2 };
// Output = 853032
int t = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
String ij = String.valueOf(arr[i]) + String.valueOf(arr[j]);
String ji = String.valueOf(arr[j]) + String.valueOf(arr[i]);
// System.out.println("ij: " + ij);
// System.out.println("ji: " + ji);
if (Integer.parseInt(ji) > Integer.parseInt(ij)) {
t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
for simple technique
class Solution {
public:
string largestNumber(vector& nums) {
vectornums1;
string st="";
for(int i=0;i<nums.size();i++){
nums1.push_back(to_string(nums[i]));
}
for(int i=0;i<nums1.size();i++){
for(int j=i+1;j<nums1.size();j++){
if(nums1[i]+nums1[j]<nums1[j]+nums1[i])
swap(nums1[i],nums1[j]);
}
}
for(auto p:nums1){
st+=p;
}
return (st[0]=='0'?"0":st);
}
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using Permutation is taking more then 10 times time. Result is shown above