# Count of elements which form a loop in an Array according to given constraints

Given an **array A** containing **N** integers, the task is to count the number of elements which form a cycle in the array, based on the following condition.

Start to traverse the Array from index **i** and jump to its next connected index. A **directed edge exits from index i of A to index j** if **j = GCD(i, A[i]) % N**. If on traversing array in the described order, index i is again visited then index i is said to form a cycle in an array.

**Examples:**

Input:A = { 1, 1, 6, 2 }

Output:2

Explanation:

Possible traversals with the given condition are:

0 -> 1 -> 1

1 -> 1

2 -> 2

3 -> 2 -> 2

Clearly, only vertices 1 and 2 forms a cycle.

Input:A = {0, 0, 0, 6}

Output:4

Explanation:

Possible traversals with the given condition are:

0 -> 0

1 -> 1

2 -> 2

3 -> 3

Clearly, all the vertices forms a cycle.

**Approach:**

To solve the problem mentioned above, we have to assume that each **index represents a single node of the graph**.

- Each node has a single directed edge from index i of A to index j if j = GCD(i, A[i]) % n. If the traversal begins from node i.
- Node i will be called
**parent node**of this traversal and this parent node will be assigned to all the nodes visited during traversal. - While traversing the graph if we discover a node that is already visited and parent node of that visited node is same as parent node of the traversal then a new cycle is detected.
- Now, every node in this cycle will be counted as each of them is forming the cycle. To count number of nodes in this cycle, start another DFS from this node until this same node is not visited again.
- This procedure is repeated for every node i of the graph.

In the worst case, every node will be traversed at most 3 times. Hence solution has linear time complexity.

Below is the implementation of above approach:

`// C++ program to number of elements ` `// which form a cycle in an array ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define mp make_pair ` `#define pb push_back ` `#define mod 1000000007 ` ` ` `// Function to count number of ` `// elements forming a cycle ` `int` `solve(` `int` `A[], ` `int` `n) ` `{ ` ` ` `int` `i, cnt = 0, j; ` ` ` ` ` `// Array to store parent ` ` ` `// node of traversal. ` ` ` `int` `parent[n]; ` ` ` ` ` `// Array to determine ` ` ` `// whether current node ` ` ` `// is already counted ` ` ` `// in the cycle. ` ` ` `int` `vis[n]; ` ` ` ` ` `// Initialize the arrays. ` ` ` `memset` `(parent, -1, ` `sizeof` `(parent)); ` ` ` `memset` `(vis, 0, ` `sizeof` `(vis)); ` ` ` ` ` `for` `(i = 0; i < n; i++) { ` ` ` `j = i; ` ` ` ` ` `// Check if current node is already ` ` ` `// traversed or not. If node is not ` ` ` `// traversed yet then parent value ` ` ` `// will be -1. ` ` ` `if` `(parent[j] == -1) { ` ` ` ` ` `// Traverse the graph until an ` ` ` `// already visited node is not ` ` ` `// found. ` ` ` `while` `(parent[j] == -1) { ` ` ` `parent[j] = i; ` ` ` `j = __gcd(j, A[j]) % n; ` ` ` `} ` ` ` ` ` `// Check parent value to ensure ` ` ` `// a cycle is present. ` ` ` `if` `(parent[j] == i) { ` ` ` ` ` `// Count number of nodes in ` ` ` `// the cycle. ` ` ` `while` `(!vis[j]) { ` ` ` `vis[j] = 1; ` ` ` `cnt++; ` ` ` `j = __gcd(j, A[j]) % n; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `cnt; ` `} ` ` ` `int` `main() ` `{ ` ` ` `int` `A[] = { 1, 1, 6, 2 }; ` ` ` `int` `n = ` `sizeof` `(A) / ` `sizeof` `(A[0]); ` ` ` `cout << solve(A, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity: ** O(N)

**Space Complexity: ** O(N)

## Recommended Posts:

- Check loop in array according to given constraints
- Count of sub-arrays whose elements can be re-arranged to form palindromes
- Minimum elements inserted in a sorted array to form an Arithmetic progression
- Count of array elements which is smaller than both its adjacent elements
- Add elements of given arrays with given constraints
- Find an element in an array such that elements form a strictly decreasing and increasing sequence
- Count number of elements between two given elements in array
- Count array elements that divide the sum of all other elements
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count of elements in first Array greater than second Array with each element considered only once
- For each element in 1st array count elements less than or equal to it in 2nd array
- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- Form an array of distinct elements with each element as sum of an element from each array
- Count of even and odd set bit Array elements after XOR with K for Q queries
- Count elements present in first array but not in second

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.