Anishluke92 / RubySolution

Problem solving using Ruby programming language
0 stars 1 forks source link

Sharing is Caring #9

Closed danielpaul closed 3 years ago

danielpaul commented 4 years ago

Given an array of numbers, create a function that removes 25% from every number in the array except the smallest number, and adds the total amount removed to the smallest number.

Examples

show_the_love([4, 1, 4]) ➞ [3, 3, 3]

show_the_love([16, 10, 8]) ➞ [12, 7.5, 14.5]

show_the_love([2, 100]) ➞ [27, 75]

Notes

There will only be one smallest number in a given array.

Anishluke92 commented 4 years ago

Algorithm.

  1. 25 % can be written as 0.25
  2. We have a set of numbers, [4,1,4] and we need to remove the 25% of each number in that set.
  3. first lets find the number which the smallest in the set.
  4. To check whether the number is smallest number or not. For that, when we multiply 0.25 with any number if we should get a result less than 1 then it is called smallest number or else its a bigger number. 4 Then we calculate 25% for the bigger numbers, Example lets take the first number 4 and then multiple it with 0.25 which gives us 1. then we need to subtract 1 out of 4 to get 3. Further we should replace 4 with 3.
  5. To the Last, the smallest number for that we need to add the 25% of the rest of the numbers to the smallest number. Example [4,1,4] new Smallest number = 25 % of 4 + smallest number + 25 % of 4 3 = 1+1+1
  6. then our result should be [3,3,3]
danielpaul commented 4 years ago

@Anishluke92 too technical and uses a specific example. Similar to my other comment in the other issue, rewrite algorithm to be without an example and no array used.

Anishluke92 commented 4 years ago

Updated algorithm.

  1. We have a set of numbers.
  2. Check which number is the smallest in that set. Except that smallest number , we need to remove 25% of the remaining biggest numbers.
  3. For that we take the first biggest number and remove the 25% of that number and the number is being updated. Similarly do that for all the remaining biggest numbers.
  4. Now each 25% of the biggest numbers is added along with the smallest number and further the smallest is been updated.