Currently I got a problem in which we have two arrays say x=[x1,x2,x3,...,xn]
and array y=[y1,y2,y3,...,yn]
and a value say k. Now I have to generate an array say z=[z1,z2,z3,...,zn]
from k, such that z1+z2+z3...+zn=k
. For different z generated what will be the minimum value of max of [(x1-z1)*y1, (x2-z2)*y2, (x3-z3)*y3, ...., (xn-zn)*yn]
. i.e minimum value of maximum of (x[i]-z[i])*y[i]
. For e.g. if x=[2,3,4,1,6]
and y=[3,5,2,7,3]
and k=4 than taking z=[0,1,0,0,3]
gives array [6,10,8,7,9]
for which maximum is 10
which is also minimum maximum.
I designed an algorithm which computes it in O(nlog(n)+k)
.Here if k will be very large than my algorithm will be inefficient. Can we do it in O(n)
or O(nlog(n))
.
My Current Algorithm is:
1. l=[] //initialize empty array
2. for i from 0 to n:
l.append(x[i]*y[i],y[i])
3. Sort l in decreasing order of (x[i]*y[i])
4. while(m>0):
num=l[0][0]-l[1][0] //take difference of two largest x[i]*y[i]
t=(num/l[0][1])+1 //Choose appropriate number to subtract to minimize
the maximum
t=max(0,t) // t must not be negative
l[0][0]=l[0][0]-t*l[0][1]
Put l[0] at correct position in sorted list l //Since value of
l[0][0] has
changed we will
place it at
correct position
in already sorted
l (using binary
search)
m=m-t
5.Print l[0][0] as the minimum maximum