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.
here, we have to equalize the number of chocolates that each person has. I have categorized the solution into four steps -
1> first thing you have to understand is that giving some chocolates to everyone except one is same as taking away chocolates from one of them. for example -
Out of 4 people in a competition,3 gets selected for next round --> 1 person loses.
Out of 4 people in a competition, 1 person loses --> 3 gets selected for next round.
2> Using the set of numbers which are given in the question (1,2,5), we can reduce any given value(say 201) to any desired value(say 0,10,37,99....upto 200).So, logically, we would want to reduce the rest of the numbers in the array to the minimum value in it and calculate the number of steps.
3>Now,we require the minimum number of steps, so for every element in array,first we will find the difference between the minimum value in array with the current element and divide it by 5(start with largest number in the set to smallest), then the remainder is divided by 2 and then by 1 and count the number of steps.
2 2 3 7 --> 2 2 2 7 --> 2 2 2 2 (2 steps)
4>But, here comes the tricky part. lets say the input is -
3 6 6.
Based on our logic,
3 6 6 --> 3 4 6 --> 3 3 6 --> 3 3 4 --> 3 3 3 (4 steps)
but, there is another minimal way
3 6 6 --> 1 6 6 --> 1 1 6 --> 1 1 1 (3 steps)
So, we will have to check for all the values (min,min-1,min-2,min-3 and min-4) to find out the best possible solution out of all the options.
Hope it helps!
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Equal
You are viewing a single comment's thread. Return to all comments →
here, we have to equalize the number of chocolates that each person has. I have categorized the solution into four steps -
1> first thing you have to understand is that giving some chocolates to everyone except one is same as taking away chocolates from one of them. for example - Out of 4 people in a competition,3 gets selected for next round --> 1 person loses. Out of 4 people in a competition, 1 person loses --> 3 gets selected for next round.
2> Using the set of numbers which are given in the question (1,2,5), we can reduce any given value(say 201) to any desired value(say 0,10,37,99....upto 200).So, logically, we would want to reduce the rest of the numbers in the array to the minimum value in it and calculate the number of steps.
3>Now,we require the minimum number of steps, so for every element in array,first we will find the difference between the minimum value in array with the current element and divide it by 5(start with largest number in the set to smallest), then the remainder is divided by 2 and then by 1 and count the number of steps.
2 2 3 7 --> 2 2 2 7 --> 2 2 2 2 (2 steps)
4>But, here comes the tricky part. lets say the input is - 3 6 6. Based on our logic, 3 6 6 --> 3 4 6 --> 3 3 6 --> 3 3 4 --> 3 3 3 (4 steps)
but, there is another minimal way 3 6 6 --> 1 6 6 --> 1 1 6 --> 1 1 1 (3 steps)
So, we will have to check for all the values (min,min-1,min-2,min-3 and min-4) to find out the best possible solution out of all the options.
Hope it helps!