Form Biggest Number

I’m not sure what’s the problem with my code.It doesn’t run for few of the test cases.

@armaanbhardwaj23 try this test and you’ll know
1
4
98 990 998 999
also try to use a simple appraoch which is viable to justify during interviews!

I know my code is incorrect and a bit complex.I’ve already tried and taken a few other custom inputs for which my code wasn’t working.So,I don’t know how to approach this problem.

Alright Armaan, so first let’s think what can be done for 2 numbers:
99 and 98,
first option is 9998 and second is 9899, obviously first one is better, so now consider 3 or more numbers and you will see that it boils down to normal sorting problem where comparison between two numbers is judged on concatenation.
To be more precise if concat(a,b)>concat(b,a) then a should come before b.
Try to code it.

1 Like

is the concat function present in stl

for integers NO, I wrote it this way only to make you understand the concept,
but it’s easy to implement yourself!
convert int to string using to_string()
now
bool compare(int &a,int &b){
string A = to_string(a);
string B = to_string(b);
return A+B>B+A;
}

1 Like

yeah I got it thanks!

Happy to help, Keep Coding!!

1 Like