## Interview Question

**Country:**United States

If y = 1000 and z = 1000, then even a fully loaded truck would lose all the weapons before reaching its destination, so the output should be 0.

The code above, however, does not account for a possible not fully loaded last trip. Example:

x = 3500

y = 1000

z = 100

Output should be 3100 but the code will output 2700

void f(){

dp[0]=w;

for(int i=0;i<=d;i++){

for ( int j = i+1; j <=d;j++){

int x = (dp[i]/c)*(j-i),y = (dp[i]%c>=j-i)? (j-i) : dp[i]%c ;

dp[j] = max(dp[j],dp[i]-x-y );

}

}

cout<<dp[d];

}

```
public class MaxWeapons {
private static int getMaxWeapons(int x, int y, int z) {
if (x < y) {
return Math.max(x - z, 0);
}
int count = x;
for (int i = 1; i <= z; i++) {
count = count - (int) Math.ceil(((double) count) / y);
if (count <= 0) {
break;
}
}
return Math.max(0, count);
}
public static void main(String[] args) {
System.out.println(getMaxWeapons(3000, 1000, 1000));
System.out.println(getMaxWeapons(1000, 1200, 1));
System.out.println(getMaxWeapons(1000, 1200, 100));
}
}
```

To get maximum weapons to the destination we can move all the weapons to one KM and then again full load the truck and carry. So we need to keep doing unloading/loading at 1 KM of distance.

- AEK September 07, 2019