Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created June 21, 2020 08:41
Show Gist options
  • Save misterpoloy/f944fde7d0897cddf961d6655559da66 to your computer and use it in GitHub Desktop.
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
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;
}
@misterpoloy
Copy link
Author

CodeChallenge

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