Greedy algorithm (Find out maximum number) | Sololearn: Learn to code for FREE!

+1

Greedy algorithm (Find out maximum number)

Help me please problem :- I want to make max number out of digit Solution 1:- sort the list in descending order But the problem is if there are 2 digit 2 21 suppose So,ans is 212 But correct ans should be 221 Solution 2:- make permutations of given no and then find max but it is exceeding time limit of 5 sec Pls provide a approach . Example:- Input 9 4 6 1 9 Output:-99641 In this case normal sorting can be done and then reverse --------------------------------------------------- Example 2:- Input:-21 2 Output :- 221 If I used above algorithm ie sort >>2 21 Reverse>>212 The answer is wrong Output :- 221. ------------------------------------------------------------------------ Input:- 23 213 Output:-23213 https://code.sololearn.com/cOVy49jjv3rW/?ref=app https://code.sololearn.com/cYMyc7oxa12A/?ref=app

5/17/2020 1:48:09 PM

Priyal Vyas

70 Answers

New Answer

+4

Alexander Thiem Priyal rodwynnejones Suppose we have an array n=[5,67,98,26,1] 1. Check which is the maximum number & what it's length We got 98 with length 2 2. Add 1 in length Now length became 3 3. Increase the length of each element by concatenating & truncating itself to it Old = New 5 = 555 67 = 676 98 = 989 26 = 262 1 = 111 4. Sort the new values in descending order We'll get [989,676,555,262,111] 5. Place the old value instead of new values We'll get [98,67,5,26,1] 6. Concatenate each element & you'll get your answer Hope You Understood Thanks for asking πŸ€—πŸ€—πŸ€—

+3

Priyal Will you put an example? & I think you are giving a challenge or asking for the readymade code Plz clearify it, so that ppl can help you further

+2

Alexander Thiem Priyal Use this πŸ‘‡πŸ‘‡πŸ‘‡ def sort(num) : for j in range(len(num)) : for i in range(len(num)-j-1) : if num[i]>num[i+1] :num[i],num[i+1]=num[i+1],num[i] return num num=input().split() #5 67 98 26 1 print("".join((sort(num))[::-1]))

+2

Guys: I coded my solution: have fun https://code.sololearn.com/cMfCYlv7SYzX/?ref=app

+2

Priyal Alexander Thiem Use this πŸ‘‡πŸ‘‡πŸ‘‡ def la(b): p, d = [], "" l=len(str(max(b))) + 1 for i in b: s = str(i)*l p.append((s[:l:], i)) p.sort(reverse = True) for i in p: d += str(i[1]) return d a=[int(i) for i in (input().split())] print(la(a)) Hope it helped πŸ€—πŸ€—

+2

Priyal yeah sure if you have read the first statement of Alexander Thiem , it completely makes the solution of problem, you can solve this by own but no worry, I'm here to help you

+2

Seems to be correctπŸ‘πŸ‘

+2

Dearfriend i wrote it on python too. second one is on cpp https://code.sololearn.com/c9b7Ns0Zdoxr/?ref=app https://code.sololearn.com/cuOBRK90uSA9/?ref=app

+1

You have to change the way it is sorted!! In the sort algorithm 2 has to be threaten as 22222222 and 21 as 2121212121.... Then 2 is bigger as 21..... Have fun

+1

Priyal Look at my answer! It solves this

+1

comment deleted....just realised what your actually trying to do.

+1

You mean 100 Numbers??? Of course it is just O(n)+ sortalgorithm(with selfmace comparison function). So i Think it should work

+1

Ok I will code it hopefully tomorrow....

+1

Priyal Use this πŸ‘‡πŸ‘‡πŸ‘‡ n=input().split() m=[] for i in n: for j in i:m.append(str(j)) m.sort() d="".join(m) print(d[::-1])

+1

πŠπ’π’π›π¨ π†π‘πšπ²πšπ₯ I think this was the first approach only working for numbers<10

+1

Priyal I think your new code would not work for 7 9 8 as input. Output 978 Suggested output 987

+1

I tested both mine and the one you posted, mine took 10 time longer to run using 10 two digit numbers so need to think how else to do it.

+1

That is what your code says, but it should be output : 232 You shpuöd not spöit the numbers

+1

Alexander Thiem I think I have taken the question in another way Plz tell me , its output 5 67 098 26 1

+1

To be honest I dont knoe what happens with 098, but 5 67 98 26 1 will result in 98675261