Form the biggest number problem

https://ide.codingblocks.com/s/230407 please heck, this code is runnning fine for me but all test cases are getting failed?

@rachitbansal2500
the correct comparator
bool compare(string a,string b){
return a + b > b+a; //return by which you get a bigger number.
}

Hey! I don’t understand why is it so?

think about β€œ909” and β€œ99”
99 < 909 yields true.
but we will put 99 first and then 909 to form the biggest number.

But when we do string comparisons it’s always lexicographical hence a>b will actually yield 909<99

Test case that fails a>b is
1
3
9 990 909
we are not comparing lexicographically. and also to sort lexicographically we can use
bool compare(string a, string b) {

return lexicographical_compare(a.begin(), a.end(), b.begin(), b.end()); //return lexo smaller

}

1 Like

Ok I do understand why lexicographical sort is not the correct option. But we can simply perform lexicographical sort using a>b in string comparisons right?

Yes. we can do that.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.