I came across this question in recent interview :
Given an array of integers and an integer k. You can perform operations any number of times(may be zero) on any number of elements in an array :
If number is divisible by k, divide it by k (you are not allowed to multiply it by k if it is divisible)
If number is not divisible by k, multiply it by k
You need to update this array in such a way that difference between maximum and minimum number in an array is minimum and find minimum number of operations to do so.
e.g. Let a[5] = {82, 79, 38, 49, 9} and k=5, we apply 2nd operation on last element. Now, a[5] = {82, 79, 38, 49, 9*5} and this updated array gives minimum difference between minimum and maximum numbers i.e. max - min = 82 - 38 = 44
I am thinking about applying recursive solution fixing one number and trying to keep all numbers as close as fixed number.
But need better approach to solve this problem efficiently. Thanks in advance.