# Weekly Coding Challenge #3

by Matthew Baptist. Posted on Oct 07, 2020For this week’s code challenge, we’ll be taking a simple look at a common problem in Computer Science.

For this problem, you’re going to read in an array that has an unknown number of cycles in it. In this case, each element in the array points to the index of another element. A simple array with one cycle where each element points to the next element would look like:

index: 0 | 1 | 2 | 3 | 4

————————————

value: 1 | 2 | 3 | 4 | 0

If an element of the array is pointing to its own index, then it’s a one-element cycle. So the array

index: 0 | 1 | 2 | 3 | 4

————————————

value: 0 | 1 | 2 | 3 | 4

is considered to have 5 different one-element cycles.

**Challenge:** Read a series of integers from the standard input into an array, (with the end of the sequence denoted by -1), and determine how many cycles there are in the resulting array.

Ex: The series: 1, 0, -1

Would give array:

index: 0 | 1

——————–

value: 1 | 0

Which has one cycle.

You can test your program on your own inputs and then on this sample input (which should give you 8 cycles).

http://www.summertech.net/wp-content/uploads/2020/10/cycles.txt