#include<bits/stdc++.h>
using namespace std;
int concact(int a ,int b)
{
string s1 ,s2;
s1 = to_string(a);
s2 = to_string(b);
string s=s1+s2;
int c = stoi(s);
return c;
}
int main() {
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
int val1=0;
int val2=0;
int temp=0;
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
val1=concact(arr[i],arr[j]);
val2=concact(arr[j],arr[i]);
if(val2>val1)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(int i=0;i<n;i++)
{
cout<<arr[i];
}
}
return 0;
}
I think my logic is fine
hello @siddhant_samal your codeis completely fine but as the test cases are larger keeping in mind this you have to think of some optimised approach .
you code complexity in big O(n^2) notation is this .
there is one optimised approach in this in which you can do this simply you can do what you can check by comparing two number (i.e if you have two numbers a and b then you can check by concatenating ‘a’ with ‘b’ and ‘b’ with ‘a’ and you can return the maximum of both .
this way you can do until the end of the array and by using the sort function with custom comparator function.
Here for your reference i am attaching the code https://ide.codingblocks.com/s/344737
if you have any query in the understanding of the concept you can ask here i will explain .
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.