Can you tell me what is the problem in my code for FORM BIGGEST NUMBER question

#include <bits/stdc++.h>

using namespace std;

bool compare(string a,string b){

return a>b;

}

int main()
{

int t;
cin>>t;

while(t--){

int n;
cin>>n;
cin.get();
 string a[n];

for(int i=0;i<n;i++){
        cin>>a[i];
}


sort(a,a+n,compare);



for(int i=0;i<n;i++){
    cout<<a[i];

}

cout<<endl;


}



return 0;

}

hey @sahilkhan2312000131 please share your code by saving it one ide.codingblocks.com

I am able to pass only the test case given in question.


try now .

I do not understand why have you made changes to comparator like this:
bool compare(string a,string b){

return a+b>b+a;

}
Can you please explain

this is because you have to check whether if you are appedning a to b is bigger then when you are appending b to a .
this you have to check at every step because you have to form the biggest number possible .

What is the problem in this:
bool compare(string a, string b){
return a>b;
}
This will also compare the two strings lexicographically and will give correct answer.

yes this will only compare the lexicographically but this will not form the biggest number possible we can form with these numbers.

Can you explain using any test case where it will not work??

hey @sahilkhan2312000131 suppose the input is :
548 60 546
then according to your code it will return only the sorted form in increasing order but it should return the largest number possible and it will be formed if we take 60 in the front because 6 series is ofcourse greater then 5 series .

if you feel that you doubt is cleared please mark this doubt as resolved and do hit the like button.
Happy Learning !!

hey @tusharaggarwal272
For this case, my code will give output as 60548546 which is the correct output.
Can we discuss this on call because we already have written 11 comments and it will take less time.

@sahilkhan2312000131
1
2
20 2

check for this case.
output should be 220 urs will give 202

@aman212yadav are you taking this doubt ?

no bro … just helped him with the case

okay fine :slight_smile:
@sahilkhan2312000131 he has provided you with the correct example .
have you understood the difference ?

yes…thank you so much

hey @sahilkhan2312000131 if you feel that you doubt is resolved please mark this doubt as resolved and do hit give the stars rating .
Happy Learning !!