Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SuryaPratapK/e7211a71677eb30933ae9c22fda6bb55 to your computer and use it in GitHub Desktop.
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;
}
@milan1404
Copy link

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)

@kyoy2k
Copy link

kyoy2k commented Aug 10, 2021

#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!

@Jeevesh-Joshi
Copy link

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

@kairanishanth
Copy link

please provide python code for nlogn without importing the permutations

@Bhargav1224
Copy link

please attach javascript code

@abhibhatia98
Copy link

abhibhatia98 commented Sep 12, 2022

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

@sheetalrm209
Copy link

sheetalrm209/Programs@cdee004

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

@anusha-yadav
Copy link

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