# Squirrel Nut Problem

A Squirrel has to transport nuts from location A to location B. The distance between two locations is “X” kms. The Squirrel will consume one nut for every 1 km it travels. The Squirrel can carry at max “Y” nuts at a given time. If there are “Z” nuts at location A, what is the maximum number of nuts that can be transported to location B.

Write a program to calculate the maximum number of nuts that a Squirrel can transport - given X,Y & Z

Total distance – X

Total nuts that a Squirrel can carry – y

Total nuts available at location - Z

Input

String (X, Y, Z) 200, 200, 600

Output

50

Explanation

Given 600 nuts, Squirrel can carry a maximum of 200 nuts at a time. The total distance to cover is 200. To start with if a Squirrel takes 200 nuts and walks 200 kms, it will not be left with any nuts at the end of 200 kms, since it consumes 1 nut per km.

Note 50 is not the best possible answer

asked Dec 3, 2016

```// C++ program to find the maximum no. of nuts that a Squirrel can transport
#include <bits/stdc++.h>
using namespace std;

// Function to calculate maximum no of nuts transported.
// x - Total distance
// y - Total nuts that a Squirrel can carry or capacity of the Squirrel
// z - Total nuts available at location
void maxTransport(int x, int y, int x)
{
// If distance is greater than the capacity,
//   then squirrel will end up eating all nuts on the way
if (x > y)
{
return 0;
}
//Check if available nuts is greater than distance.
// this is to transport all available nuts to the cache or the destination location
while ( z > x)
{
// if available nuts after transport is greater than distance,
// it means the transport has to be continued
if( (z - y ) > x)
z = z/2;
else
z = y;
}
// when the available is equal to distance ,
// it means the squirrel can directly transport all available to the destination
if ( z == x )
z = z/2;

return z;
}

// Driver code
int main()
{
int x, y, z;
cout<<"Enter the distance between Location A and B";
cin>>x;
cout<<"Enter the Total nuts the Squirrel can carry";
cin>>y;
cout<<"Enter the total nuts available at Location A";
cin>>z;
int result = maxTransport(x, y, z);
cout<<" The maximum no. of nuts the squirrel can transfer is "<<result;
return 0;
}```

answered Dec 3, 2016