I came across an interview question asked at Google which I can not solve:
There is a pile of
Nkg grains at an oasis located in a distance ofDkm to a town. The grains need to be transported by a camel cart whose initial location is at the oasis. The cart can carryCkg of grains at a time. The camel uses the grains as fuel while transporting them. It consumesFkg/km.Write a function that computes the maximum amount of grains (
Xkg) that can be transported to the town.
I tried to use recursion but I couldn’t get much further without confusing myself.
Here’s what I have so far:
number of transports = N / C
fuel amount for distance D = D * F
X = N - ((number of transports) * 2 * (fuel amount for distance D))
Assuming that N, D, C and F are inputs, –