#include <iostream>
using namespace std;
int dig1_max(int );
void num_max(int *,int *);
main()
{
int n,temp,*arr,a,b;
cout<<"Enter number of non-negative numbers to be arranged : ";
cin>>n;
arr=new int[n];
cout<<("\nEnter the numbers to be arranged : ")<<endl;
for(int k=0;k<n;k++)
cin>>arr[k];
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1;j++)
{
if(dig1_max(arr[j])<dig1_max(arr[j+1]))
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
else if(dig1_max(arr[j])==dig1_max(arr[j+1]))
{
a=arr[j];
b=arr[j+1];
num_max(&a,&b);
arr[j]=a;
arr[j+1]=b;
}
else{}
}}
cout<<"The largest number formed combining entered numbers is ";
for(int z=0;z<n;z++)
cout<<arr[z];
}
int dig1_max(int x)
{
int n=0;
while(x>0)
{
n=x%10;
x=x/10;
}
return(n);
}
void num_max(int *m,int *n)
{
int s=1,k,c=0,num1,num2,p,q;
p=*m;
q=*n;
k=q;
for(;k>0;c++)
{
k=k/10;
}
for(;c>0;c--)
{
s=s*10;
}
num1=p*s+q;
k=p;
c=0;
for(;k>0;c++)
{
k=k/10;
}
s=1;
for(;c>0;c--)
{
s=s*10;
}
num2=q*s+p;
if(num1>num2)
{
*m=p; *n=q;
}
else
{
*m=q; *n=p;
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwppbnQgZGlnMV9tYXgoaW50ICk7CnZvaWQgbnVtX21heChpbnQgKixpbnQgKik7Cm1haW4oKQp7CmludCBuLHRlbXAsKmFycixhLGI7CmNvdXQ8PCJFbnRlciBudW1iZXIgb2Ygbm9uLW5lZ2F0aXZlIG51bWJlcnMgdG8gYmUgYXJyYW5nZWQgOiAiOwpjaW4+Pm47CmFycj1uZXcgaW50W25dOwpjb3V0PDwoIlxuRW50ZXIgdGhlIG51bWJlcnMgdG8gYmUgYXJyYW5nZWQgOiAiKTw8ZW5kbDsKZm9yKGludCBrPTA7azxuO2srK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