human
Check if the array can be sorted only if the elements on given positions can be swapped | Function to check if the array can be sorted only if the elements on the given positions can be swapped.
Creating an array for marking the positions.
Iterating through the array and mark the positions.
Iterating through the given array.
If pos [ i ] is 1 , then incrementing till 1 is continuously present in pos.
Sorting the required segment.
Checking if the vector is sorted or not.
Print yes if it is sorted.
Driver code"
Solution in Python:
 Can you provide a code to check if the array can be sorted only if the elements on given positions can be swapped and the other steps required?
assistant
#Here is code to check if the array can be sorted only if the elements on given positions can be swapped and the other steps required.
 def check_vector ( A , n , p ) :
	pos = [ 0 for i in range ( len ( A ) ) ]
	for i in range ( len ( p ) ) :
		pos [ p [ i ] - 1 ] = 1
	flag = 1
	for i in range ( n ) :
		if ( pos [ i ] == 0 ) :
			continue
		j = i
		while ( j < n and pos [ j ] ) :
			j += 1
		p = A [ : i ]
		q = A [ i : i + j + 1 ]
		r = A [ i + j + 1 : len ( A ) ]
		q . sort ( reverse = False )
		A = p + q + r
		i = j
	for i in range ( n - 1 ) :
		if ( A [ i ] > A [ i + 1 ] ) :
			flag = 0
			break
	if ( flag == 1 ) :
		print ( " Yes " )
	else :
		print ( " No " ) ;
if __name__ == ' _ _ main _ _ ' :
	A = [ 3 , 2 , 1 ]
	p = [ 1 , 2 ]
	check_vector ( A , len ( A ) , p )