i cannot go about how to write the code for this problem
in the solution. I am not able to grasp the concept or the code.
kindly help
Form biggest number
For this problem you can treat the array’s elements as strings and sort them in lexicographically decreasing order. For eg. [54, 546, 548, 60] if you sort these in lexicographically decreasing order result will be [60, 548, 546, 54] , then you can print all the elements and it will form the biggest number.
So take input in the form of string. Make your own custom comparator and sort accordingly.
The custom compare function must be something like this:
bool mycompare(string a,string b)
{
string ab=a+b;
string ba=b+a;
return ab>ba;
}
Consider the first two elements of the above array:
So in the compare function:
a=54
b=546
ab=54546
ba=54654
Since ba has a larger value than ab, so you have to sort such that b comes before a in the array.
So we return ab>ba (if ab forms the larger number a must come before b in the array and if ba forms the larger number b must come before a in the array)