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.


Input: A = { 1, 1, 6, 2 }
Output: 2
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
Possible traversals with the given condition are:
0 -> 0
1 -> 1
2 -> 2
3 -> 3
Clearly, all the vertices forms a cycle.


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;
                    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;




Time Complexity: O(N)

Space Complexity: O(N)


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to 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.