Arrange given numbers to form the biggest number
Form Biggest Number
Arrange given numbers to form the biggest number
Form Biggest Number
Hello @rprahulpal03 the simple logic you can apply in this is that take the sort function and take compare in it .
in that compare function return the max of
a+b or b+a .
in this way you can do this question.
Here for your reference i am attaching the code:
Happy Learning !!
#include
#include
using namespace std;
bool compare(string a,string b){
return a+b>b+a;
}
int main()
{
int t;
cin>>t;
for (; t > 0; 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];
}
}
}
not able to pass any testcase
#include<bits/stdc++.h>
using namespace std;
bool compare(string a,string b){
return a+b>b+a;
}
int main()
{
int t;
cin>>t;
for (; t > 0; 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;
}
}
corrected code:
you have to print for every output in new line.
Happy Learning!!
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.