human
Find integral points with minimum distance from given set of integers using BFS | Function to find points at minimum distance.
Hash to store points that are encountered.
Queue to store initial set of points.
Vector to store integral points.
Using bfs to visit nearest points from already visited points.
Get first element from queue.
Check if ( x - 1 ) is not encountered so far.
Update hash with this new element.
Insert ( x - 1 ) into queue.
Push ( x - 1 ) as new element.
Decrement counter by 1.
Check if ( x + 1 ) is not encountered so far.
Update hash with this new element.
Insert ( x + 1 ) into queue.
Push ( x + 1 ) as new element.
Decrement counter by 1.
Print result array.
Driver code"
Solution in Python:
assistant
#Here is code to find integral points with minimum distance from given set of integers using BFS and the other steps required.
 def minDistancePoints ( A , K , n ) :
	m = { }
	q = [ ]
	for i in range ( n ) :
		m [ A [ i ] ] = 1
		q . append ( A [ i ] )
	ans = [ ]
	while ( K > 0 ) :
		x = q [ 0 ]
		q = q [ 1 : : ]
		if ( ( x - 1 ) not in m and K > 0 ) :
			m [ x - 1 ] = m . get ( x - 1 , 0 ) + 1
			q . append ( x - 1 )
			ans . append ( x - 1 )
			K -= 1
		if ( ( x + 1 ) not in m and K > 0 ) :
			m [ x + 1 ] = m . get ( x + 1 , 0 ) + 1
			q . append ( x + 1 )
			ans . append ( x + 1 )
			K -= 1
	for i in ans :
		print ( i , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
	A = [ - 1 , 4 , 6 ]
	K = 3
	n = len ( A )
	minDistancePoints ( A , K , n )