Ad

This Question Is Asked In A Coding Test. I Am Unable To Find The Solution Till Now

- 1 answer

An array of integer is given of size n. We have to make all the equal. For this we can add 1, 2 or 5 to the array element any time and to any element. We have to find minimum no of operation to do so?

For example Array. 2 2 3 7

Output should be 2

Explanation In first operation we add 1 to 2,2,7 After that array will be as 3 3 3 8 Now in 2nd operation we add 5 to 3,3,3 After that array will be as 8,8,8,8

Ad

Answer

You could first think about how many operations it would cost to get all values to the highest value in the list.

In a second step you can think if there could be a better solution if you try to reach highest-value+1, highest-value+2, highest-value+3, highest-value+4 .

Ad
source: stackoverflow.com
Ad