human
Minimize the number of steps required to reach the end of the array | Set 2 | Function to return the minimum steps required to reach the end of the given array.
Array to determine whether a cell has been visited before.
Queue for bfs.
Push the source i . e . index 0.
Variable to store the depth of search.
BFS algorithm.
Current queue size.
Top - most element of queue.
Base case.
If we reach the destination i . e . index ( n - 1 ).
Marking the cell visited.
Pushing the adjacent nodes i . e . indices reachable from the current index.
Driver code"
Solution in Python:
 ... Can you provide a code to minimize the number of steps required to reach the end of the array and the other steps required?
assistant
#Here is code to minimize the number of steps required to reach the end of the array and the other steps required.
 def minSteps ( arr , n ) :
	v = [ 0 for i in range ( n ) ]
	q = [ ]
	q . append ( 0 )
	depth = 0
	while ( len ( q ) != 0 ) :
		x = len ( q )
		while ( x >= 1 ) :
			i = q [ 0 ]
			q . remove ( i )
			x -= 1
			if ( v [ i ] ) :
				continue ;
			if ( i == n - 1 ) :
				return depth
			v [ i ] = 1
			if ( i + arr [ i ] < n ) :
				q . append ( i + arr [ i ] )
			if ( i - arr [ i ] >= 0 ) :
				q . append ( i - arr [ i ] )
		depth += 1
	return - 1
if __name__ == ' _ _ main _ _ ' :
	arr = [ 1 , 1 , 1 , 1 , 1 , 1 ]
	n = len ( arr )
	print ( minSteps ( arr , n ) )