human
Sorting array with conditional swapping | Function to check if it is possible to sort the array.
Calculating max_element at each iteration ..
if we can not swap the i - th element ..
if it is impossible to swap the max_element then we can not sort the array ..
Otherwise , we can sort the array ..
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to sorting array with conditional swapping and the other steps required.
 def possibleToSort ( arr , n , str ) :
	max_element = - 1
	for i in range ( len ( str ) ) :
		max_element = max ( max_element , arr [ i ] )
		if ( str [ i ] == '0' ) :
			if ( max_element > i + 1 ) :
				return " No "
	return " Yes "
if __name__ == " _ _ main _ _ " :
	arr = [ 1 , 2 , 5 , 3 , 4 , 6 ]
	n = len ( arr )
	str = "01110"
	print ( possibleToSort ( arr , n , str ) )