Elegant approach O(n) / 100% / Illustrations
From the first look at this task, you immediately understand here definitely must be some logical approach and you\'re right, let\'s start!\n\nThe first thing w
From the first look at this task, you immediately understand here definitely must be some logical approach and you\'re right, let\'s start!\n\nThe first thing we need to understand - **we don\'t need to keep track of all previous visits except the last loop**, because if we have:\n```\nn=4 rounds=[1,4]\n```\nthe result will be just one loop and no intersection which means all sectors are equal 1.\n```\nn=4 rounds=[1,4,1,4]\n```\nIn this example, all sectors are equal to 2, which means we can skip this fancy staff and concentrate only on the last loop.\n\nThe most interesting part is coming when we start an intersection, let\'s visualize that. There is gonna be only two cases:\n1) the first sector is less than the last sector\n![image](https://assets.leetcode.com/users/images/7120a793-f034-433b-90a1-aebec71a793b_1645609952.060334.png)\n\n2) the first sector is greater than the last sector\n![image](https://assets.leetcode.com/users/images/86da8d5f-e2cf-4820-afff-e419f5a8c503_1645612067.9527416.png)\n\nDid you spot the pattern?\n\nWhen the first sector is less than the second we skip all completed loops and just add sectors that appear in the last loop between the first and last sector, first and last, sectors are included too.\n\nWhen the first sector is greater than the second we do skipping the same way but this time we add sectors from the first till the last, which means there will be one jump from the end to the beginning.\n\n```\nvar mostVisited = function(n, rounds) {\n    const first = rounds[0];\n    const last = rounds[rounds.length - 1];\n   \n    const result = [];\n   \n    if (first <= last) {\n        for (let i = last; i >= first; i--) result.unshift(i)\n    } else {\n        for (let i = 1; i <= last; i++) result.push(i);\n        for (let i = first; i <= n; i++) result.push(i);\n    }\n   \n    return result;\n};\n```
['JavaScript']