Form biggest no. related doubt

code crux:
using pair<int,int>
making every element’s length equivalent to length of maximum element by multiplying it by 10 and storing it’s value in pair.second to further sort on that basis.

problem:
getting correct output in custom cases but not getting the mistake that none of my testcase is correct.

#include
#include
using namespace std;
int length(pair<int,int> num)
{
int count=0;
while(num.first!=0)
{
num.first=num.first/10;
count++;
}
return count;
}
bool compare(pair<int,int>a,pair<int,int>b)
{
return a.second>b.second;
}
int main() {

int t;
cin>>t;
while(t)
{
	int m;
	cin>>m;
	int a[100000];
	pair <int,int> p[100000];
	for(int i=0;i<m;i++)
	{
		cin>>a[i];
		p[i].first=a[i];
		p[i].second=a[i];
	}
	pair<int,int> max;
	int *k;
	 k= max_element(a,a+m);
	 max.first=(*k);
	 max.second=max.first;
	int len=length(max);

	for(int i=0;i<m;i++)
	{
		int temp=length(p[i]);
		if(temp<len)
		{
			int mul=len-temp;
		
			while(mul)
			{
			p[i].second*=10;
			mul--;
			}
		}
	}
sort(p,p+m,compare);
for(int i=0;i<m;i++)
{
	cout<<p[i].first;
}
cout<<endl;
t--;

}
return 0;

}

hello @jsunidhi

consider this case->
1
2
2 21

here expected output is 221 but urs will give 212.

the logic is if a and b are two numbers then u can combine them in two ways either ab or ba right?
which ever combination give us maxium we will consider that order.
so just apply same logic in ur comparator also store numbers as strings it will be easy to handle.

bool cmp(string a,string b){
return a+b > b+a;
} 

ok, got it. Thank you

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.