It passed the given example but is failing the given test case.
Can you please provide me with a little hint?
Here’s my code - https://ide.codingblocks.com/s/110047
Wrong answer for FORM BIGGEST NUMBER
@ranjanpanda hey rajan this will not work. Instead of doing this do sort with custom comparator.
Well actually I hardly have knowledge of comparator. Can you please help me with it?
@ranjanpanda hey rajan
Try this logic
int myCompare(string X, string Y)
{
// first append Y at the end of X
// then append X at the end of Y
// Now see which of the two formed numbers is greater
return XY.compare(YX) > 0 ? 1: 0;
}
Sir this time my code seems to be completely correct, but is still showing wrong answer.
Here is my code - https://ide.codingblocks.com/s/110705
Yeah thank you, it worked. But can you explain me the comparator thing and how does a comparator work.
@ranjanpanda hey rajan
A simple solution that comes to our mind is to sort all numbers in descending order, but simply sorting doesn’t work. For example, 548 is greater than 60, but in output 60 comes before 548. As a second example, 98 is greater than 9, but 9 comes before 98 in output.
So how do we go about it? The idea is to use any comparison based sorting algorithm.
In the used sorting algorithm, instead of using the default comparison, write a comparison function myCompare() and use it to sort numbers.
Given two numbers X and Y, how should myCompare() decide which number to put first – we compare two numbers XY (Y appended at the end of X) and YX (X appended at the end of Y). If XY is larger, then X should come before Y in output, else Y should come before. For example, let X and Y be 542 and 60. To compare X and Y, we compare 54260 and 60542. Since 60542 is greater than 54260,
we put Y first.
So sir over here we are comparing the strings lexicographically. So by that logic my very first code should have been correct https://ide.codingblocks.com/s/110047 ?
@ranjanpanda here our main motto to arrange our number to form biggest no. here is the link check the lexicographical order of digits. lexicographical order of digits have some patterns.