human
Sort integers in array according to their distance from the element K | Function to get sorted array based on their distance from given integer K.
Vector to store respective elements with their distance from integer K.
Find the position of integer K.
Insert the elements with their distance from K in vector.
Element at distance 0.
Elements at left side of K.
Elements at right side of K.
Print the vector content in sorted order.
Sort elements at same distance.
Print elements at distance i from K.
Driver code"
Solution in Python:
assistant
#Here is code to sort integers in array according to their distance from the element K and the other steps required.
 def distanceSort ( arr , K , n ) :
	vd = [ [ ] for i in range ( n ) ]
	for i in range ( n ) :
		if ( arr [ i ] == K ) :
			pos = i
			break
	i = pos - 1
	j = pos + 1
	vd [ 0 ] . append ( arr [ pos ] )
	while ( i >= 0 ) :
		vd [ pos - i ] . append ( arr [ i ] )
		i -= 1
	while ( j < n ) :
		vd [ j - pos ] . append ( arr [ j ] )
		j += 1
	for i in range ( max ( pos , n - pos - 1 ) + 1 ) :
		vd [ i ] . sort ( reverse = False )
		for element in vd [ i ] :
			print ( element , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 14 , 1101 , 10 , 35 , 0 ]
	K = 35
	n = len ( arr )
	distanceSort ( arr , K , n )