JavaScript solution with explanation, using prefix sum.
\n## Explanation\n\nThe idea:\n\n1. Create prefix sum of distance.\n2. With the prefix sum, you can easily get the distance between start and destination in one
\n## Explanation\n\nThe idea:\n\n1. Create prefix sum of `distance`.\n2. With the prefix sum, you can easily get the distance between `start` and `destination` in one direction (either clockwise or counterclockwise).\n3. To get the distance for the other direction, you can subtract the distance above from the total sum distance.\n\n## Code\n\n```javascript\nconst sortBy = require(\'lodash/sortBy\')\n\nconst getPrefixSum = (distance) => {\n  const prefixSum = [0]\n\n  for (let i = 0; i < distance.length; i++) {\n    prefixSum.push(prefixSum[i] + distance[i])\n  }\n\n  return prefixSum\n}\n\nconst distanceBetweenBusStops = (distance, start, destination) => {\n  const prefixSum = getPrefixSum(distance)\n\n  const [startIndex, endIndex] = sortBy([start, destination])\n  const totalSum = prefixSum[prefixSum.length - 1]\n\n  const clockwise = Math.abs(prefixSum[endIndex] - prefixSum[startIndex])\n  const counterClockwise = totalSum - clockwise\n\n  return Math.min(clockwise, counterClockwise)\n}\n\nmodule.exports = distanceBetweenBusStops\n```\n\n## Complexity Analysis\n\nTime complexity: O(n) since we loop through all elements in `distance` exactly once.\n\nSpace complexity: O(1). Extra space used is always the same.\n
['JavaScript']