Not getting logic

i am not getting proper logic for the question .can anyone please help me

hey @anishravula660 you should learn about comparators it is a very important topic.
This question also requires the knowledge of comparators.
a normal person will think of sorting the array to produce maximum number. but this approach is wrong.
think this way , suppose you are given an array [52,62,7] . if you sort the array you will get 62 52 7 but answer should be 7 62 52 as it is the largest no. that can be formed.

while sorting we use comparator and in comparator we make each array element as string and sort using lexicographical order we concatenate the array values and check which value gives maximum value for eg if array is [7,62] then we find if 762 is bigger value or 627 is greater then we update our result accordingly .

more precisely,
Given two numbers X and Y , how should comparator 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.

refer to the code for more understanding

int myCompare(string X, string Y)

{

string XY = X.append(Y);

string YX = Y.append(X);

return XY.compare(YX) > 0 ? 1 : 0;

}

void printLargest(vector<string> arr)

{

sort(arr.begin(), arr.end(), myCompare);

for ( int i = 0; i < arr.size(); i++)

cout << arr[i];

}

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.