human
Minimize Steps required to obtain Sorted Order of an Array | Function to find GCD of two numbers.
Function to calculate the LCM of array elements.
Initialize result.
Function to find minimum steps required to obtain sorted sequence.
Inititalize dat [ ] array for Direct Address Table ..
Calculating steps required for each element to reach its sorted position.
Calculate LCM of the array.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to minimize Steps required to obtain Sorted Order of an Array and the other steps required.
 def gcd ( a , b ) :
	if ( b == 0 ) :
		return a
	return gcd ( b , a % b )
def findlcm ( arr , n ) :
	ans = 1
	for i in range ( 1 , n + 1 ) :
		ans = ( ( arr [ i ] * ans ) // ( gcd ( arr [ i ] , ans ) ) )
	return ans
def minimumSteps ( arr , n ) :
	dat = [ 0 ] * ( n + 1 )
	for i in range ( 1 , n + 1 ) :
		dat [ arr [ i - 1 ] ] = i
	b = [ 0 ] * ( n + 1 )
	j = 0
	for i in range ( 1 , n + 1 ) :
		c = 1
		j = dat [ i ]
		while ( j != i ) :
			c += 1
			j = dat [ j ]
		b [ i ] = c
	print ( findlcm ( b , n ) )
arr = [ 5 , 1 , 4 , 3 , 2 , 7 , 6 ]
N = len ( arr )
minimumSteps ( arr , N )