## 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.

```
public static long findMaxWeapons(long X, long Y, long Z) {
if(X <= Y)
return X - Z;
long wh;
if( X % Y == 0)
{
if(Y % (X / Y) == 0)
wh = Y / (X/Y);
else
wh = Y / (X/Y) + 1;
}
else
{
if((X % Y) % (X / Y + 1) == 0)
wh = (X % Y)/(X / Y + 1);
else
wh = (X % Y)/(X / Y + 1) + 1;
}
if(wh >= Z)
{
if(X % Y == 0)
return X - X * Z/ Y;
else
{
long rem = X % Y;
long ini = X - X * Z / Y;
if(rem > Z)
ini = ini + rem - Z;
return ini;
}
}
else
return findMaxWeapons(X - (((X % Y) == 0)? X/Y : X/Y + 1) * wh , Y , Z - wh);
```

}

public static long findMaxWeapons(long X, long Y, long Z) {

if(X <= Y)

return X - Z;

long wh;

if( X % Y == 0)

{

if(Y % (X / Y) == 0)

wh = Y / (X/Y);

else

wh = Y / (X/Y) + 1;

}

else

{

if((X % Y) % (X / Y + 1) == 0)

wh = (X % Y)/(X / Y + 1);

else

wh = (X % Y)/(X / Y + 1) + 1;

}

if(wh >= Z)

{

if(X % Y == 0)

return X - X * Z/ Y;

else

{

long rem = X % Y;

long ini = X - X * Z / Y;

if(rem > Z)

ini = ini + rem - Z;

return ini;

}

}

else

return findMaxWeapons(X - (((X % Y) == 0)? X/Y : X/Y + 1) * wh , Y , Z - wh);

}

- AEK September 07, 2019