Only Test Case 0 passed | FORM BIGGEST NUMBER

This is my code.
At first I have sorted the array by passing a function as the third parameter for comparing digits at each position. Then I printed the array in reverse order.

DESCRIPTION OF PARAMETER FUNCTION:
At first I converted the two integers a, b (the parameters in the comparator function) to strings A and B. String A is a and string B is b. Then at each index i checked if there is inequality. In case inequality found i return A[i] < B[i]. If there is equality I increment i till either end of A or B is reached. If end is reached i exit the loop, and then check, if size of A is same as size of B. If yes, i return A.size() == B.size(). Else , if A is bigger than B with respect to size, I check if last character of B is bigger than next character of A. Similar check is performed if B is bigger than A with respect to size.

I am unable to think of a test case where my logic will fail. But I am able to pass test case 0 only.

Please Help.

@manisini.chakraborty for the input
600 60
the expected answer is 60600 but your code is giving answer as 60060

I changed the code. Now for this particular input, i.e 600 60 or 60 600, my output is correct. I guess there is no other case left which i have not dealt with. Still i am not able to pass beyond test case 0. Help!!

^^ My changed code ^^

@manisini.chakraborty your comparator function is too complicated
consider everything to be a string (so as to make comparison easier and to avoid overflow)
then the compare function can be

bool compare(string a, string b) {
    return a+b > b+a;
}
1 Like

Oh yes! Thank you ! It worked !

1 Like