We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

Thanks. Got it.
But there is an cal error in your operations. Instead of 12 12 12 (19) 19 19, it should be 12 12 12 (17) 17 17.
Therefore, finally it would be 27 27 27 27 27 27.

logic is about making equal not zero.So in 2 5 5 5 5 5.If we reduce 3 from all 5 we left with each having 2 chocolate and that's what we want equal chocolates.

Thanks.But I got some other method which would solve by 4 operations please suggest any loop holes in my metghod.
step 1:place the values in the list
step 2:[1,3,5] are the possible ways to give chocolates
step 3: calculate minimum_of_list and maximum_of_list and index where maximum_of_list is present in list(if we have multiple maximum_of_list it has to return first_index)
step 4:diff = maximum_of_list - minimum_of_list
step 5:add the diff to all the list elements except to maximum_of_list
step 6:repeat step 3 to step 5 until all the values are equal in alist
Example:
(x):assume x is a small value
[x]:assume x is maximum_of_list
2 5 5 5 5
(2) [5] 5 5 5
(5) 5 [8] 8 8
(8) 8 8 [11] 11
(11) 11 11 11 [14]
14 14 14 14 14

It doesn't give optimal solution in all cases.
TC => 2 5 5 5 5 5
expected=> 6
actual=>10

I implemented it so I know. The problem occurs because we are always adding to lower elements which causes problems as it is not proved to be safe move.

## Equal

You are viewing a single comment's thread. Return to all comments →

(2) 5 5 5 5 5

2 (7) 7 7 7 7

7 7 (12) 12 12 12

12 12 12 (19) 19 19

19 19 19 19 (24) 24

24 24 24 24 24 (29)

29 29 29 29 29 29

It's a same choice.

Thanks. Got it. But there is an cal error in your operations. Instead of 12 12 12 (19) 19 19, it should be 12 12 12 (17) 17 17. Therefore, finally it would be 27 27 27 27 27 27.

Possible in 5 step.

You Miss this condition

you give chocolate bars to all but chosen one.so you wont able to give chocolates to all at once.They should really bold this in the question. I didn't realize this making the problem seem very hard for me.

In second step, U have added 3, but addition of 1, 2 and 5 is ony possible

logic is about making equal not zero.So in 2 5 5 5 5 5.If we reduce 3 from all 5 we left with each having 2 chocolate and that's what we want equal chocolates.

what's woring in it?

We need to add to all except one element. You are adding to just one element.

Not correct, you can only give 1, 2 or 5 chocolates, not 3

12 12 12 (19) 19 19 19 19 19 19 (24) 24 ???

we can end it at 13 13 13 13 13 13 also but operations required are 6 only.

Thanks.But I got some other method which would solve by 4 operations please suggest any loop holes in my metghod. step 1:place the values in the list step 2:[1,3,5] are the possible ways to give chocolates step 3: calculate minimum_of_list and maximum_of_list and index where maximum_of_list is present in list(if we have multiple maximum_of_list it has to return first_index) step 4:diff = maximum_of_list - minimum_of_list step 5:add the diff to all the list elements except to maximum_of_list step 6:repeat step 3 to step 5 until all the values are equal in alist Example: (x):assume x is a small value [x]:assume x is maximum_of_list 2 5 5 5 5 (2) [5] 5 5 5 (5) 5 [8] 8 8 (8) 8 8 [11] 11 (11) 11 11 11 [14] 14 14 14 14 14

Have you implemented this approach??

the ways to give choclates is {1,2,5} and not {1,3,5}

That's not what the problem states.

I guess you figured out that statement has changed since. It was {1,2,5} two months ago

You cannot add 3 to any number. # will be added as combination of 1 and 2 which leads to 2 operation. Eveny my approach was same but got it wrong.

Could you please tell me how to solve 1 2 3 4 5 6

somebody changed the problem to [1,3,5] from [1,2,5] but didn't change the solutions

Yup.

In bird culture that is considered a dick move

Spent a few hours debugging my solution before going to forum in desperation and reading this comment...

Has this been resolved now?

if the difference is other than 1,2,5 how will you proceed next??

This would have slow performance.

It doesn't give optimal solution in all cases. TC => 2 5 5 5 5 5 expected=> 6 actual=>10

I implemented it so I know. The problem occurs because we are always adding to lower elements which causes problems as it is not proved to be safe move.

Also it give TLE.

yup according to

edemohankrishna1

Shouldn't be 3 2 3 5 7 1) 7 8 10 (7) 2) 10 11 (10) 10 3) 11 (11) 11 11

How did you decide you need to add 2 for the first iteration and 5 for the second iteration ?

(2) 5 5 5 5 5 (5) 5 8 8 8 8 (8) 8 8 11 11 11 (11) 11 11 11 14 14 (14) 14 14 14 14 17 (17) 17 17 17 17 17

only 5 steps

Yeah, with the differene method, 5 is coming only. But, in the test case it is saying 6 steps

@singhavdeshk for 2 5 5 5 5 5 operartion count will not be 6 it will be 5 2 5 5 5 5 5 5 5 8 8 8 8 8 8 8 11 11 11 11 11 11 11 14 14 14 14 14 14 14 17 17 17 17 17 17 17

I've done the same thing that you have shown over here. But, for, say, 1 5 5, By this method, we get: (1) 5 5 (+ 0 steps) 1(5) 5 (+0 steps) 5 5 (9) (+2 steps) 9 9 9 (+2 steps) = 4 steps. However, in some other manner: (1) 5 5 (+0 steps) 1 (5) 5 (+0 steps) 6 5 (10) (+1 steps) sort() (5) 6 10 (+0 steps) 5 (6) 10 (+0 steps) 6 6 (11) (+1 steps) 11 11 11 (+1 steps) = 3 steps

The actual answer for 1,5,5 is 3 but i get 4. Please shed a light on this, im still confused