JS - O(N) - Detailed explanation, beginers friendly
Intuition\nThe main idea to solve this problem is pretty simple:\n\nThere is a circle path so we have always two ways for getting from point A to point B: clock
# Intuition\nThe main idea to solve this problem is pretty simple:\n\nThere is a circle path so we have always two ways for getting from point A to point B: clockwise and counterclockwise.\n\nWe can count total distance easily. Just get sum of the all **distance** array elements.\n\nThen we need to count the distance one of the paths. No matter direction.\n\nAnd finally we will be able to get the distance of the second direction easily: \n\nclockwise distance = total distance - counterclockwise distance (and vice versa)\n\n# Approach\nFirst of all let\'s get the total distance:\n```\nlet totalDistance = distance.reduce((a,b) => a+b,0);\n```\n\nThen we need to count a distance for one of the paths.\n\nWe have two cases: \n1) When the start position less than the destionation position \n2) and vice versa.\n\nCase 1 (when the start position (index 0) less then the destination (index 1))\n[]()\n![help-1.png](https://assets.leetcode.com/users/images/93ec5295-7408-4797-9a73-25a5940864ea_1674919909.8148787.png)\nCase 2 (when the start position (index 5) more than the destination position (index 2) )\n![help-2.png](https://assets.leetcode.com/users/images/252ad6cb-676f-4a81-aa80-bc13a8619d31_1674921973.5201592.png)\n\nTo get the distance one of the paths we should start loop from **including** start position to **not including** destination position and sum every distance[i] item in our counter *clockwiseDistance*.\n\nFor example, the first case above we start from 0 index and stop on the 1 index **not including**. (red color) Because index 1 is the part of another paths. It\'s marked with the different colors for your convenience.\n\nNote: "Position" word is the same as index of the **distance** array in our context.\n```\nlet clockwiseDistance = 0;\n```\nWe don\'t know what case we will have. It can be that start position will be more than destination. To work a loop correctly we can use Math.min() and Math.max() methods to solve this issue.\n```\nlet begining = Math.min(start, destination);\nlet finish = Math.max(start, destination);\n```\nAnd fill one of the paths distance:\n```\nfor (let i = begining; i < finish; i++) {\n    clockwiseDistance += distance[i];\n}\n```\nNow we know the distance one of the paths. We can count the second path distance with the help of the total distance we\'ve counted above.\n```\nlet counterclockwiseDistance = totalDistance - clockwiseDistance;\n```\nAnd finaly we just need to choose the least distance because we have both.\n```\nreturn Math.min(clockwiseDistance, counterclockwiseDistance);\n```\n\n# Complexity\n- Time complexity: O(N)\n\n- Space complexity: O(N)\n\n# Code\n```\n/**\n * @param {number[]} distance\n * @param {number} start\n * @param {number} destination\n * @return {number}\n */\nvar distanceBetweenBusStops = function(distance, start, destination) {\n    let totalDistance = distance.reduce((a,b) => a+b,0);\n    let clockwiseDistance = 0;\n    \n    let begining = Math.min(start, destination);\n    let finish = Math.max(start, destination);\n     \n    for (let i = begining; i < finish; i++) {\n        clockwiseDistance += distance[i];\n    }\n\n    let counterclockwiseDistance = totalDistance - clockwiseDistance;\n\n    return Math.min(clockwiseDistance, counterclockwiseDistance);\n};\n```
['Array', 'JavaScript']