-
-
Save SuryaPratapK/e7211a71677eb30933ae9c22fda6bb55 to your computer and use it in GitHub Desktop.
//#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; | |
} |
If someone came here looking for Javascript implementation
function sortArray(arr){
arr.sort(function compare(first,second) {
var firstsecond ='' + first + second;
var secondfirst ='' + second + first;
return firstsecond>secondfirst ? -1:1;
})
}
function getLargestNumber(arr){
var largestNumber = arr.join('')
return largestNumber
}
var arr = [13,2,4]
sortArray(arr)
var result = getLargestNumber(arr)
console.log(result)
Pls attach Python code
def check(nums):
maximum=str(max(nums))
new_l=len(maximum)2
l=[]
for i in nums:
value=str(i)(new_l//len(str(i)))
l.append((value,str(i)))
res=''
for k in sorted(l,reverse=True):
res+=k[1]
result=int(res)
return str(result)
#python code
def largestnumber(a):
largestnumber =''
for i in range(len(a)-1):
for j in range(i+1, len(a)):
temp1 = str(a[i])+str(a[j])
temp2 = str(a[j])+str(a[i])
if int(temp2) > int(temp1):
a[i] , a[j] = a[j] , a[i]
for i in range(len(a)):
largestnumber += str(a[i])
return int(largestnumber)print(largestnumber([3,30,34,9])
i login to give you a thumbup!
Pls attach Python code
from functools import cmp_to_key
class Solution:
def largestNumber(self, nums: List[int]) -> str:
def comp(x,y):
if int(str(x)+str(y))>int(str(y)+str(x)):
return -1
else:
return 1
nums.sort(key=cmp_to_key(comp))
ans=""
for i in nums:
ans+=str(i)
if ans[0]=='0':
return "0"
return ans
please provide python code for nlogn without importing the permutations
please attach javascript code
@Bhargav1224 here it is in the comment above :) https://gist.github.com/SuryaPratapK/e7211a71677eb30933ae9c22fda6bb55?permalink_comment_id=3650870#gistcomment-3650870
Pls attach Python code
from itertools import permutations def largest(l): lst = [] for i in permutations(l, len(l)): # provides all permutations of the list values, # store them in list to find max lst.append("".join(map(str,i))) return max(lst)
print(largest([54, 546, 548, 60])) #Output 6054854654
from itertools import permutations
def permutation_method(l):
lst = []
for i in permutations(l):
lst.append("".join(map(str, i)))
return max(lst)
def normal_method(l):
for i in range(len(l)):
for j in range(i+1, len(l)):
temp1 = int(str(l[i]) + str(l[j]))
temp2 = int(str(l[j]) + str(l[i]))
if temp1 < temp2:
l[i], l[j] = l[j], l[i]
return "".join(str(i) for i in l)
# print(largest_2([54, 546, 548, 60])) # Output 6054854654
# print(largest([54, 546, 548, 60])) # Output 6054854654
import timeit, random
m = 1000000
testdata = [random.randrange(m + 1) for _ in range(6)]
assert permutation_method(testdata)
assert normal_method(testdata)
print(timeit.timeit("f(ab)", "from __main__ import permutation_method as f, testdata as ab", number=1000))
print(timeit.timeit("f(ab)", "from __main__ import normal_method as f, testdata as ab", number=1000))
#0.8338875
#0.01934020000000003
Using Permutation is taking more then 10 times time. Result is shown above
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);
}
};
String printLargest(String[] arr) {