Test cases are not passing

could you check if my logic is correct?

first i took in all the elements then i sorted the array.

with the sort function i passed a comparator function

which first converts the elements to string and then compares the string (in lexicographically order) and then returned back the value.

this will give us the highest possible numbers i think since in strings it is being compared in lexicographical order

then i printed the new sorted array

hello @ashishxmathew

the comparator logic is not correct.
for example ->
1
2
2 21

the output is 212 but expected output is 221.

ur comparator should look like this->

bool compare(int a,int b)
{
	string s1=to_string(a);
	string s2=to_string(b);
	return (s1+s2>s2+s1);
}

so here this is making both the strings of same length (by adding s1 to s2 and vice versa) and then we are comparing the strings right.?

also is this the best approach or is there a better one?

yeah if we have two number a and b then we can order them in two ways
ab or ba and for our problem we should consider that combintation which give maximum number .
that why we defined comparator like this.

no it is the effiicient one

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.