FORM BIGGEST NUMBER problm

AM GETTING WRONG ANSWER ON SUBMISSION EVEN THOUGH MY CODE WORKS FOR THE GIVEN SAMPLE CASE
my code
https://ide.codingblocks.com/s/41647

question link:
https://hack.codingblocks.com/contests/c/512/1291

PLEASE TELL ME MY MISTAKE

Your code does not handle all cases.
eg. for input
1
4
9 5 90 6

Your output is
90965

But the correct output must be
99065

Make changes in the compare function

1 Like

MODIFIED THE SORT AS MUCH AS POSSIBLE AND I HAVE COMMENTED MY STEPS AS WELL FOR UR CONVENIENCE

https://ide.codingblocks.com/s/41737

AM STILL GETTING A BIT FAULTY OUTPUT WITH THIS INPUT
1
5
9 94 9423 94 949

bool compare(string a,string b){

    if(a+b>b+a)
    return true;
    else
    return false;

}

Try to change your compare function like this and analyze this code . You will definitely get a new concept.

Hit like if u get it :stuck_out_tongue:

3 Likes

THANKS A LOT I TOTALLY UNDERSTOOD THIS AND
JUST ONE THING THAT
CAN U PLEASE GIVE ME A LOGICAL EXPLANATION OF MY MISTAKE CAUSE OTHERWISE I MAY REPEAT THAT IN FUTURE IN SOME OTHER PROBLEM

The error in your code is the string comparison that you are doing
a > b
“>” : is overloaded in string class but the way it compare string is : it one by one traverses two string (starting from 0 ) then at any point it finds character (i.e at any index ) greater than other it outputs that string as greater .

for example 8 is greater than 600 according to this comparison (as 8>6).
I hope this clears your doubt, hit like if you get it! :stuck_out_tongue:
cheers!

bhaiya
am too traversing one by one and returning a>b i.e true if i found the inequality or else false
and i have also placed a condition to make sure i dont overshoot the smaller string"s end index
https://ide.codingblocks.com/s/41737

AND ONE MORE THING
DOES SORT FUNCTION USE OUR CUSTOMIZED “COMPARE” FUNCTION MANY TIMES OR JUST ONE TIME
I MEAN HOW DOES COMPARE FUNCTION MANIPULATE INBUILT SORT FUNCTION
I JUST KNOW HOW TO MANIPULATE BUT DONT KNOW HOW IT WORKS INTERNALLY

I was talking about error in this code: https://ide.codingblocks.com/s/41647

bool compare(string a,string b){

    return a>b;

}

Is this the comparison is wrong that is why you are having the wrong answer.

Sort function uses your customized always whenever you provide in the code.
In inbuilt sort function, their is a function used for comparison, in case a user does not specify it, it uses a default, if the user specifies it then it uses the compare function always.

Hit like if you get it! :stuck_out_tongue:
cheers!

please tell my error in this code cause i made this after my sort function was giving 90>9 as output which is correct but for forming biggest number 9>90 is required

BASICALLY I MODIFIED COMPARE FUNCTION SO THAT I CAN FORM THE BIGGEST NUMBER BUT THE SORT FUNCTION ISNT SORTING AND I DONT KNOW WHY

IN THIS ONE
https://ide.codingblocks.com/s/41737

not in this
https://ide.codingblocks.com/s/41647

you are making it too complex. Rather than using the recursion in the compare function. Try using for loop for incrementing x.
If a.size()!=b.size() bt upto min(a.size(),b.size())
they are same like this
a=25
b=253
Then u have to compare the b[a.size()] with a[0]
as b[a.size()]=3 and a[0]=2

.
.
.
.
.
.
Bt implement it using your code is having too many corner cases. So better than implementing according to your compare function. Try the above given compare function as it was completing all corner cases.

Check once again your code is returning correct answer. see Screenshot
But your code will fail and show run time error because of to many recursive calls.

i have put a loop instead of repetitive recursive calls as u suggested but still its giving faulty output i.e
949 94 9423 94 9

for this input
1
5
9 94 9423 94 949

https://ide.codingblocks.com/s/42143

because in if(x==i) condition there will be many corner cases which u cant rectify using normal if else. So instead of using normal compare function , use the above mentioned compare function

am just immitating what the sort function would do in
return a+b>b+a

i have not yet understood why my way wont work so
please can u give me an example of corner case for which my code wont work

at a=55 b=552 and a=55 b=557
In both the cases In your code you are saying that a+b is smaller.

where as in first case b+a is smaller and in second case a+b is smaller

It is not easy to immitate that compare function as that function is directly completely many corner cases