human
Longest subset of nested elements from a given array | Function to find length of longest subset such that subset { arr [ i ] , arr [ arr [ i ] ] , . . }.
Stores length of the longest subset that satisfy the condition.
Traverse in the array , arr [ ].
If arr [ i ] equals to i.
Update res.
Count of elements in a subset.
Stores index of elements in the current subset.
Calculate length of a subset that satisfy the condition.
Make visited the current index.
Update curr_index.
Update count.
Update res.
Driver Code"
How can the above be solved in Python?
 ... Give me a code to longest subset of nested elements from a given array and the other steps required?
assistant
#Here is code to longest subset of nested elements from a given array and the other steps required.
 def arrayNesting ( arr ) :
	res = 0
	for i in range ( len ( arr ) ) :
		if arr [ i ] == i :
			res = max ( res , 1 )
		else :
			count = 0
			curr_index = i
			while arr [ curr_index ] != curr_index :
				next_index = arr [ curr_index ]
				arr [ curr_index ] = curr_index
				curr_index = next_index
				count += 1
		res = max ( res , count )
	return res
if __name__ == " _ _ main _ _ " :
	arr = [ 5 , 4 , 0 , 3 , 1 , 6 , 2 ]
	res = arrayNesting ( arr )
	print ( res )