File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programming Diversions and the fly likes Programming Puzzle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Programming Puzzle" Watch "Programming Puzzle" New topic

Programming Puzzle

mani vannan
Ranch Hand

Joined: Apr 17, 2006
Posts: 47
Problem Description:

There are N petrol bunks arranged in circle. Each bunk is separated from the rest by a certain distance. You choose some mode of travel which needs 1 litre of petrol to cover 1 km distance. You can't infinitely draw any amount of petrol from each bunk as each bunk has some limited petrol only. But you know that the sum of liters of petrol in all the bunks is equal to the distance to be covered.
i.e let P1, P2, ... Pn be n bunks arranged circularly. d1 is distance between p1 and p2, d2 is distance between p2 and p3. dn is distance between pn and p1.Now find out the bunk from where the travel can be started such that your mode of travel never runs out of fuel.

Also, derive the time complexity and space complexity measures.

Mani (SCEA 1.4)<br /><a href="" target="_blank" rel="nofollow"> Is SCJP Useful? Worth? Helps job?</a>
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
sounds computationally equivalent to the Traveling Salesman Problem

Bill Shirley - bshirley -
if (Posts < 30) JavaRanchFAQ);
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3028
I think it may be a good deal simpler than the traveling salesman, if my guesses about the intent here are correct.

[mani]: But you know that the sum of liters of petrol in all the bunks is equal to the distance to be covered.

What distance to be covered? It seems like there are many possible distances that we might travel, depending on our objectives - and the problem hasn't even given us a goal. Perhaps we are supposed to visit each fuel station once? Perhaps the "distance to be covered" is the total d1 + d2 + d3 + ... + dn? In this case, we could simply travel the circle (or polygon as the case may be), visiting each station in order, never reversing direction. The only important choices would be (a) where to start, and (b) which direction to travel in, so as not to ever run out of fuel. There's a reasonably simple algorithm that can be used here, and there will always be at least two solutions - one in each direction. Maybe more.
I agree. Here's the link:
subject: Programming Puzzle
jQuery in Action, 3rd edition