We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
You are correct, you only need to run your loop from 0 to less than arr.length - 1 because either:
1. The number which is supposed to go in the last index is in the first n - 1 indices, in which case we put it in its correct place at the last index
or
2. The number which is supposed to go in the last index is already in the last index, in which case we do not need to look at it.
Therefore, it does not matter that we don't look at the last index.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Swaps 2
You are viewing a single comment's thread. Return to all comments →
You are correct, you only need to run your loop from 0 to less than arr.length - 1 because either: 1. The number which is supposed to go in the last index is in the first n - 1 indices, in which case we put it in its correct place at the last index or 2. The number which is supposed to go in the last index is already in the last index, in which case we do not need to look at it.
Therefore, it does not matter that we don't look at the last index.