What am I doing wrong?

#include<bits/stdc++.h>
using namespace std;
int main() {
int t; cin >> t;
while(t > 0){
int n; cin >> n;
int arr[n];
for(int i = 0; i < n; i++){
cin >> arr[i];
}
int num_digits[n];
int max_digits = 0;
for(int i =0; i < n; i++){
int x = arr[i];
int count = 0;
while(x > 0 ){
x /= 10;
count++;
}
max_digits = max(max_digits, count);
num_digits[i] = count;
}
int val[n];
for(int i = 0; i < n; i++){
val[i] = arr[i] * pow(10, max_digits - num_digits[i]);
}
for(int i = 0; i < n -1; i++)
for(int j = 0; j < n-1-i; j++){
if(val[j] < val[j+1]){
swap(val[j], val[j+1]);
swap(num_digits[j], num_digits[j+1]);
}
}

	for(int i = 0; i < n; i++){
		cout << val[i] / pow(10, max_digits - num_digits[i]);
	}
	t--;
}
return 0;

}

Sample test cases working perfectly, other test cases not working.

You are trying to maximise the value by just checking the larger number among the two and then concatenating them.
But rather you should check the numbers by taking the maximum of a+b or b+a .
Eg : 990 , 9.
ans = 9990

How do I treat numbers as strings in c++?

Let us suppose you have to arrange two numbers A and B to obtain the larger number. You will have to decide which number to put first – we compare two numbers AB ( B appended at the end of A) and BA (A appended at the end of B). If AB is larger, then A should come before B in output, else B should come before. You can append them as strings for comparison.

this is basically a string sort question
where u make a custom comparator and
u neeed to make the comparison of 2 integers by converting to string
return a+b > b+a
eg:
a = 9
b = 90
so to form biggest number a+b -> 990
and b+a -> 909
so which is bigger 990
even though 90 is numerically greater than 9

so the crux is to return a+b > b+a
a & b are string value of int being passed as paramter

1 Like

just one doubt, can you explain int to string and string to int conversion in c++? I understood your soln, thanks for the explanation.

You can use string s= to_string(x).
for string to integer use int x= stoi(s)

1 Like

#include<bits/stdc++.h>
using namespace std;
int compare(int a, int b){
return stoi(to_string(a) + to_string(b)) > stoi(to_string(b) + to_string(a));
}
int main() {
int t; cin >> t;
while(t > 0){
int n; cin >> n;
int arr[n];
for(int i = 0; i < n; i++){
cin >> arr[i];
}
sort(arr, arr+n, compare);
for(int i = 0; i < n; i++ )
cout << arr[i];
t–;
}
return 0;
}

What else do You want to know.

sample test case working, rest have run error

auto mycompare = [&](string & a, string & b) {
return a + b > b + a;
};
Also dont use stoi as the numbers can get big.
You can have a look at this code for reference.

1 Like

All test cases wrong using your solution

Please help me out, no test case working through the method you’ve suggested.

Are bhai main har test case ke baad endl lagana bhul gya hu bas.
Wo laga le sahi ho jayega.

I helped you out with code just to give you the logic not the exact code. I hope now you must have understood it.

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.