printing array gives the answer but not accepting as solution then i changed to printing a num then also same problem
Even after unlocking test case it is not showing the test case
Hey @mdj your code with test case
1
2
42 4
Will give an output of: 424
But Expected output is: 442
I can see the logic isn’t familiar to our problem too, so if you want me to explain the logic, i can do that also.
we can break 42 into 4 and 2?
ok ok got it …
No we can’t break them, we have to arrange it by keeping the digit same.
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.
please help code is #include<bits/stdc++.h> using namespace std; bool comp(int a,int b){ string a1=to_string(a); string a2=to_string(b); int i=0; while(a1[i]==a2[i]){ i++; } if((int)a1[i]>(int)a2[i]){ return true; } else{ return false; } } int main() { int num; cin>>num; while(num–){ int n; cin>>n; int a[n]; for(int i=0;i<n;i++){ cin>>a[i]; } sort(a,a+n,comp); for(int i=0;i<n;i++){ cout<<a[i]; } } return 0; }
hi @mdj
A simple solution that comes to our mind is to sort all numbers in descending order, but simply sorting doesn’t work. For example, 548 is greater than 60, but in output 60 comes before 548. As a second example, 98 is greater than 9, but 9 comes before 98 in output.
So how do we go about it? The idea is to use any comparison based sorting algorithm. In the used sorting algorithm, instead of using the default comparison, write a comparison function myCompare() and use it to sort numbers. Given two numbers X and Y, how should myCompare() decide which number to put first – we compare two numbers XY (Y appended at the end of X) and YX (X appended at the end of Y). If XY is larger, then X should come before Y in output, else Y should come before. For example, let X and Y be 542 and 60. To compare X and Y, we compare 54260 and 60542. Since 60542 is greater than 54260, we put Y first.
refer this code -->
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.