Task: In a one-dimensional array, each element is an index on an element of the same array. Find and print the index of items that are "looped". Example: a[0] = 7; a[1] = 3; a[2] = 1; a[3] = 2; a[4] = 0 a[0] = 7; a[1] = 3; a[2] = 1; a[3] = 2; a[4] = 0

the elements a[1]->a[3]->a[2]->a[1] are looped.

Solution: I can not figure out where to start. If someone can give an example and / or explain, it will be great. I can not figure out the condition under which the presence of such "looped" numbers will be checked.

  • This question is completely incomprehensible, incomplete, too general, based on personal opinion or not related to the topic of Stack Overflow in Russian as described in the Help and can hardly be improved by editing. - hedgehogues
  • Well, try to go through the links-indexes until you stumble upon an element that has already been traversed. After that, look for the first missing element and repeat everything from it all the way through to exhaustion. - VladD

1 answer 1

I do not understand why this is a question. VladD essentially correctly answered, but I will write in more detail. It is necessary to get an array of bool used [n] (initially all elements are false). Then we go through the elements of the array and if the element has not yet been used, then we start the inner loop (but before that we will remember the index of this element from which we will start): go to the next element while we can, mark it in the used array if we switch to the index of the starting element - cycle found.