Count of elements which form a loop in an Array according to given constraints
Last Updated :
15 Jun, 2021
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++
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define mod 1000000007
int solve( int A[], int n)
{
int i, cnt = 0, j;
int parent[n];
int vis[n];
memset (parent, -1, sizeof (parent));
memset (vis, 0, sizeof (vis));
for (i = 0; i < n; i++) {
j = i;
if (parent[j] == -1) {
while (parent[j] == -1) {
parent[j] = i;
j = __gcd(j, A[j]) % n;
}
if (parent[j] == i) {
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;
}
|
Java
import java.util.*;
class GFG{
static final int mod = 1000000007 ;
static int solve( int A[], int n)
{
int i, cnt = 0 , j;
int []parent = new int [n];
int []vis = new int [n];
Arrays.fill(parent, - 1 );
Arrays.fill(vis, 0 );
for (i = 0 ; i < n; i++)
{
j = i;
if (parent[j] == - 1 )
{
while (parent[j] == - 1 )
{
parent[j] = i;
j = __gcd(j, A[j]) % n;
}
if (parent[j] == i)
{
while (vis[j] == 0 )
{
vis[j] = 1 ;
cnt++;
j = __gcd(j, A[j]) % n;
}
}
}
}
return cnt;
}
static int __gcd( int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
public static void main(String[] args)
{
int A[] = { 1 , 1 , 6 , 2 };
int n = A.length;
System.out.print(solve(A, n));
}
}
|
Python3
import math
mod = 1000000007
def solve(A, n):
cnt = 0
parent = [ - 1 ] * n
vis = [ 0 ] * n
for i in range (n):
j = i
if (parent[j] = = - 1 ):
while (parent[j] = = - 1 ):
parent[j] = i
j = math.gcd(j, A[j]) % n
if (parent[j] = = i):
while (vis[j] = = 0 ):
vis[j] = 1
cnt + = 1
j = math.gcd(j, A[j]) % n
return cnt
A = [ 1 , 1 , 6 , 2 ]
n = len (A)
print (solve(A, n))
|
C#
using System;
class GFG{
static int solve( int [] A, int n)
{
int i, cnt = 0, j;
int [] parent = new int [n];
int [] vis = new int [n];
Array.Fill(parent, -1);
Array.Fill(vis, 0);
for (i = 0; i < n; i++)
{
j = i;
if (parent[j] == -1)
{
while (parent[j] == -1)
{
parent[j] = i;
j = __gcd(j, A[j]) % n;
}
if (parent[j] == i)
{
while (vis[j] == 0)
{
vis[j] = 1;
cnt++;
j = __gcd(j, A[j]) % n;
}
}
}
}
return cnt;
}
static int __gcd( int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
static void Main()
{
int [] A = { 1, 1, 6, 2 };
int n = A.Length;
Console.WriteLine(solve(A, n));
}
}
|
Javascript
<script>
function solve(A, n)
{
let i, cnt = 0, j;
let parent = new Array(n);
let vis = new Array(n);
parent.fill(-1);
vis.fill(0);
for (i = 0; i < n; i++)
{
j = i;
if (parent[j] == -1)
{
while (parent[j] == -1)
{
parent[j] = i;
j = __gcd(j, A[j]) % n;
}
if (parent[j] == i)
{
while (vis[j] == 0)
{
vis[j] = 1;
cnt++;
j = __gcd(j, A[j]) % n;
}
}
}
}
return cnt;
}
function __gcd(a, b)
{
return b == 0 ? a : __gcd(b, a % b);
}
let A = [ 1, 1, 6, 2 ];
let n = A.length;
document.write(solve(A, n));
</script>
|
Time Complexity: O(N)
Space Complexity: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...