human
Rearrange given array such that no array element is same as its index | Function to rearrange the array a [ ] such that none of the array elements is same as its index.
Sort the array.
Traverse the indices [ 0 , N - 2 ] of the given array.
Check if the current element is equal to its index.
If found to be true , swap current element with the next element.
Check if the last element is same as its index.
If found to be true , swap current element with the previous element.
Prthe modified array.
Driver Code.
Function Call"
How can the above be solved in Python?
assistant
#Here is code to rearrange given array such that no array element is same as its index and the other steps required.
 def rearrangeArray ( a , n ) :
	a = sorted ( a )
	for i in range ( n - 1 ) :
		if ( a [ i ] == i + 1 ) :
			a [ i ] , a [ i + 1 ] = a [ i + 1 ] , a [ i ]
	if ( a [ n - 1 ] == n ) :
		a [ n - 1 ] , a [ n - 2 ] = a [ n - 2 ] , a [ n - 1 ]
	for i in range ( n ) :
		print ( a [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 1 , 5 , 3 , 2 , 4 ]
	N = len ( arr )
	rearrangeArray ( arr , N )