human
Magical Indices in an array | Function to count number of magical indices ..
Array to store parent node of traversal ..
Array to determine whether current node is already counted in the cycle ..
Initialize the arrays ..
Check if current node is already traversed or not . If node is not traversed yet then parent value will be - 1..
Traverse the graph until an already visited node is not found ..
Check parent value to ensure a cycle is present ..
Count number of nodes in the cycle ..
Driver code"
Solution in Python:
 ... What is a code to magical Indices in an array and the other steps required?
assistant
#Here is code to magical Indices in an array and the other steps required.
 def solve ( A , n ) :
	cnt = 0
	parent = [ None ] * ( n + 1 )
	vis = [ None ] * ( n + 1 )
	for i in range ( 0 , n + 1 ) :
		parent [ i ] = - 1
		vis [ i ] = 0
	for i in range ( 0 , n ) :
		j = i
		if ( parent [ j ] == - 1 ) :
			while ( parent [ j ] == - 1 ) :
				parent [ j ] = i
				j = ( j + A [ j ] + 1 ) % n
			if ( parent [ j ] == i ) :
				while ( vis [ j ] == 0 ) :
					vis [ j ] = 1
					cnt = cnt + 1
					j = ( j + A [ j ] + 1 ) % n
	return cnt
A = [ 0 , 0 , 0 , 2 ]
n = len ( A )
print ( solve ( A , n ) )