https://online.codingblocks.com/player/7730/content/5316?s=1583
can you explain me the approach to this problem ?
https://online.codingblocks.com/player/7730/content/5316?s=1583
can you explain me the approach to this problem ?
Can you please tell this videoβs name.
this is problem no.11 in challenges Array
In this problem if you sort all the numbers in lexicographically decreasing order then it will result into the biggest number.
for eg.
54 546 548 60
if you sort this array in lexicographically decreasing order it will become
60 548 546 54
https://ide.codingblocks.com/s/38573
i m getting the answer correct but still not able to make it according to the test case
Your code doesnβt work in many cases such as
when input is
1
4
54 546 6 60
Your output is
60654654
But the correct output should be
66054654
Instead of just sorting it lexicographically, make a sorting function like bubble sort, and make a separate function to decide if the elements need to be swapped or not.
That function should take two strings as parameters, convert them to numbers and then compare and then return bool value, depending on whether the numbers have to be swapped or not.
i m unable to do it
i didnt get it exactly
Hey Kanika, the case your code is unable to handle is when you get an input
1
4
6 60 54 546
your code gives the output : 60654654
but the biggest no. we can form using this array is : 66054654
As now your code is comparing two integers, and in comparison of integers 60 > 6. so your code treats 60 as bigger than 6 and append 6 after 60.
So, to correct this you can compare the elements of the array as strings. you can do it using c++ in-built compare() function also. It will just tell you which string is lexicographically greater.
for eg.
you have two numbers β60β and β6β. Now you want to check which gives the larger number appending β60β
after β6β or vice-versa. So, you can check this as
string x= β60β, y=β6β;
string a = y.append(x);
string b = x.append(y);
int result = a.compare(b) > 0 ? 1: 0;
the result will have the value 1 as β660β > β606β.
So by making these comparisons between the elements of array you can make decision which element you should put first.
https://ide.codingblocks.com/s/38623
this is the code after taking care of the things you told and it is also working but can u provide me with some other code(if possible) as i m a bit confused in all this.