Java Solution with clean code


#1

public static int[] solve(int A, int[] B) {
int min=B[0];
int minIndex=0;
for(int i=1;i<B.length;i++){
if(B[i]<min){
min=B[i];
minIndex=i;
}
}

int maxKicks= A/min;

int[] ans = new int[maxKicks];
for(int i=0;i<maxKicks;i++){
ans[i]=minIndex;
}

if(A%min==0){
return ans;
}

int diff= A-min*maxKicks;
int k=0;
int i=0;
// if diff we get from two index is the difference remaining the voila!
while(diff>0 && i<minIndex && k<ans.length){
if((B[i]-min)<=diff){
ans[k++]=i;
diff-=(B[i]-min);
}
else
i++;
}

return ans;