Find integral points with minimum distance from given set of integers using BFS | C # implementation of above approach ; Function to find points at minimum distance ; Hash to store points that are encountered ; Queue to store initial set of points ; List 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
using System ; using System . Collections . Generic ; class GFG { static void minDistancePoints ( int [ ] A , int K , int n ) { Dictionary < int , Boolean > m = new Dictionary < int , Boolean > ( ) ; Queue < int > q = new Queue < int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { m . Add ( A [ i ] , true ) ; q . Enqueue ( A [ i ] ) ; } List < int > ans = new List < int > ( ) ; while ( K > 0 ) { int x = q . Dequeue ( ) ; if ( ! m . ContainsKey ( x - 1 ) && K > 0 ) { m . Add ( x - 1 , true ) ; q . Enqueue ( x - 1 ) ; ans . Add ( x - 1 ) ; K -- ; } if ( ! m . ContainsKey ( x + 1 ) && K > 0 ) { m . Add ( x + 1 , true ) ; q . Enqueue ( x + 1 ) ; ans . Add ( x + 1 ) ; K -- ; } } foreach ( int i in ans ) Console . Write ( i + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] A = new int [ ] { - 1 , 4 , 6 } ; int K = 3 ; int n = A . Length ; minDistancePoints ( A , K , n ) ; } }