Gamified challenge · Bellman-Ford
There are n currencies, 0 to n - 1. Each entry [from, to, cost] of trades is a one-way exchange desk. Real rates multiply, so they have been turned into integer costs that add up: cost = round(-100 · ln(rate)). A positive cost loses money, a negative cost gains.
A free-money loop is a sequence of trades that starts and ends in the same currency with a negative total cost. Write hasArbitrage(n, trades) that returns true if any such loop exists anywhere in the market, else false. A loop totalling exactly 0 only breaks even.
Example: [[0,1,-10],[1,2,-5],[2,0,12]] with n = 3 has the loop 0 → 1 → 2 → 0 totalling -3, so the answer is true.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google