Created
June 21, 2020 08:41
-
-
Save misterpoloy/f944fde7d0897cddf961d6655559da66 to your computer and use it in GitHub Desktop.
#Code Challenge Given a number of arrays each element rempresenting the number of jumps calcultate if starts at zero element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using namespace std; | |
| int getNextIdx(int currentIdx, vector<int>& array) { | |
| int nextIdx = (currentIdx + array[currentIdx]) % (int)array.size(); | |
| return nextIdx >= 0 ? nextIdx : nextIdx + array.size(); | |
| } | |
| bool hasSingleCycle(vector<int> array) { | |
| int currentIdx = 0; | |
| int numberJumps = 0; | |
| while (numberJumps < array.size()) { | |
| // Si ha hecho saltos y no se mueve. Se quedo trabado | |
| if (numberJumps > 0 && currentIdx == 0) { | |
| return false; | |
| } | |
| numberJumps++; | |
| currentIdx = getNextIdx(currentIdx, array); | |
| } | |
| return currentIdx == 0; | |
| } |
Author
misterpoloy
commented
Jun 21, 2020

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment