Is this logic correct(Form biggest number)

First finding which number has the largest 1st digit
(like 546,60 ,output is 6) then swaping this number with a[0]
and then sort(a+1,a+n) and then printing array

#include
#include
using namespace std;
int number(int a[],int n)
{
int i,md=0,k,temp,rem,max=0;
for(i=0;i<n;i++)
{
temp=a[i];
if(md>max)
{
max=md;
k=i-1;
}
while(temp!=0)
{
rem=temp%10;
if(rem!=0)
{
md=rem;
}
temp=temp/10;
}

}
swap(a[0],a[k]);
sort(a+1,a+n);
return md;

}
int main() {
int i,t,j;
cin>>t;
for(j=0;j<t;j++)
{
int n,p;
cin>>n;
int a[n];
for(i=0;i<n;i++)
cin>>a[i];
p=number(a,n);
cout<<p;

}
return 0;

}

@tushartiwari i would suggest just sorting the strings and using a custom sort function to handle the corner cases. Please save your code on ide.codingblocks.com and elaborate your appraoch a bit more if you need help with that

@tushartiwari firstly this code is giving incorrect output. Secondly, you are storing the formed numbers in an int variable, but for bigger numbers, you cannot store it anywhere so it is better to store the formed “numbers” as strings only.

plz check this code(now it is giving correct output for sample test case

@tushartiwari you are treating all the “numbers” like int instead of string. Can you imagine how will this code process a really large input for eg, such as
6
100000 1000000 1000000 1000000 1000000 1000000

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.