Given an array arr[0..n-1] of positive and negative numbers we need to find if there is a cycle in array with given rules of movements. If a number at an i index is positive, then move arr[i]%n forward steps, i.e., next index to visit is (i + arr[i])%n. Conversely, if it’s negative, move backward arr[i]%n steps i.e., next index to visit is (i – arr[i])%n. Here n is size of array. If value of arr[i]%n is zero, then it means no move from index i.

Examples:

Input: arr[] = {2, -1, 1, 2, 2} Output: Yes Explanation: There is a loop in this array because 0 moves to 2, 2 moves to 3, and 3 moves to 0. Input : arr[] = {1, 1, 1, 1, 1, 1} Output : Yes Whole array forms a loop. Input : arr[] = {1, 2} Output : No We move from 0 to index 1. From index 1, there is no move as 2%n is 0. Note that n is 2.

Note that self loops are not considered a cycle. For example {0} is not cyclic.

The idea is to form a directed graph of array elements using given set of rules. While forming the graph we don’t make self loops as value arr[i]%n equals to 0 means no moves. Finally our task reduces to detecting cycle in a directed graph. For detecting cycle, we use DFS and in DFS if reach a node which is visited and recursion call stack, we say there is a cycle.

`// C++ program to check if a given array is cyclic or not ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// A simple Graph DFS based recursive function to check if ` `// there is cycle in graph with vertex v as root of DFS. ` `// Refer below article for details. ` `bool` `isCycleRec(` `int` `v, vector<` `int` `>adj[], ` ` ` `vector<` `bool` `> &visited, vector<` `bool` `> &recur) ` `{ ` ` ` `visited[v] = ` `true` `; ` ` ` `recur[v] = ` `true` `; ` ` ` `for` `(` `int` `i=0; i<adj[v].size(); i++) ` ` ` `{ ` ` ` `if` `(visited[adj[v][i]] == ` `false` `) ` ` ` `{ ` ` ` `if` `(isCycleRec(adj[v][i], adj, visited, recur)) ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `// There is a cycle if an adjacent is visited ` ` ` `// and present in recursion call stack recur[] ` ` ` `else` `if` `(visited[adj[v][i]] == ` `true` `&& ` ` ` `recur[adj[v][i]] == ` `true` `) ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `recur[v] = ` `false` `; ` ` ` `return` `false` `; ` `} ` ` ` `// Returns true if arr[] has cycle ` `bool` `isCycle(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Create a graph using given moves in arr[] ` ` ` `vector<` `int` `>adj[n]; ` ` ` `for` `(` `int` `i=0; i<n; i++) ` ` ` `if` `(i != (i+arr[i]+n)%n) ` ` ` `adj[i].push_back((i+arr[i]+n)%n); ` ` ` ` ` `// Do DFS traversal of graph to detect cycle ` ` ` `vector<` `bool` `> visited(n, ` `false` `); ` ` ` `vector<` `bool` `> recur(n, ` `false` `); ` ` ` `for` `(` `int` `i=0; i<n; i++) ` ` ` `if` `(visited[i]==` `false` `) ` ` ` `if` `(isCycleRec(i, adj, visited, recur)) ` ` ` `return` `true` `; ` ` ` `return` `true` `; ` `} ` ` ` `// Driver code ` `int` `main(` `void` `) ` `{ ` ` ` `int` `arr[] = {2, -1, 1, 2, 2}; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` `if` `(isCycle(arr, n)) ` ` ` `cout << ` `"Yes"` `<<endl; ` ` ` `else` ` ` `cout << ` `"No"` `<<endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output :

Yes

This article is contributed by **Roshni Agarwal**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

## Recommended Posts:

- Forming smallest array with given constraints
- Minimum steps to reach end of array under constraints
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
- Find duplicates under given constraints
- Add elements of given arrays with given constraints
- Check if an array can be Arranged in Left or Right Positioned Array
- Move weighting scale alternate under given constraints
- Check if an array is Wave Array
- Check whether an Array is Subarray of another Array
- Check whether a given array is a k sorted array or not
- Check whether the given array is perfect or not
- Check if the array is beautiful
- Check if an array contains all elements of a given range
- Check if a given array is pairwise sorted or not
- Check if the given number K is enough to reach the end of an array