# Find integral points with minimum distance from given set of integers using BFS

Given an integer array **A[]** of length **N** and an integer **K**. The task is to find **K** distinct integral points which are not present in the given array such that the sum of their distances from nearest point in **A[]** is minimized.

An

integral pointis defined as the point with both the coordinates as integers. Also, in the x-axis, we don’t need to consider y-coordinate because y-coordinated of all the points is equal to zero.

**Examples:**

Input:A[] = { -1, 4, 6 }, K = 3

Output:-2, 0, 3

Explanation:

Nearest point for -2 in A[] is -1 -> distance = 1

Nearest point for 0 in A[] is -1 -> distance = 1

Nearest point for 3 in A[] is 4 -> distance = 1

Total distance = 1 + 1 + 1 = 3 which is minimum possible distance.

Other results are also possible with the same minimum distance.

Input:A[] = { 0, 1, 3, 4 }, K = 5

Output:-1, 2, 5, -2, 6

Explanation:

Nearest point for -1 in A[] is 0 -> distance = 1

Nearest point for 2 in A[] is 3 -> distance = 1

Nearest point for 5 in A[] is 4 -> distance = 1

Nearest point for -2 in A[] is 0 -> distance = 2

Nearest point for 6 in A[] is 4 -> distance = 2

Total distance = 2 + 1 + 1 + 1 + 2 = 7 which is minimum possible distance.

**Approach:**We will solve this problem by using the concept of Breadth First Search.

- We will initially assume the given set of integers as the root element and push it in Queue and Hash.
- Then for any element say X, we will simply check if (X-1) or (X+1) is encountered or not using a hashmap. If any of them are not encountered so far then we push that element in our answer array and queue and hash as well.
- Repeat this until we encountered K new elements.

Below is the implementation of the above approach.

`// C++ implementation of above ]approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find points at ` `// minimum distance ` `int` `minDistancePoints(` `int` `A[], ` ` ` `int` `K, ` ` ` `int` `n) ` `{ ` ` ` ` ` `// Hash to store points ` ` ` `// that are encountered ` ` ` `map<` `int` `, ` `int` `> m; ` ` ` ` ` `// Queue to store initial ` ` ` `// set of points ` ` ` `queue<` `int` `> q; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; ++i) { ` ` ` `m[A[i]] = 1; ` ` ` `q.push(A[i]); ` ` ` `} ` ` ` ` ` `// Vector to store integral ` ` ` `// points ` ` ` `vector<` `int` `> ans; ` ` ` ` ` `// Using bfs to visit nearest ` ` ` `// points from already ` ` ` `// visited points ` ` ` `while` `(K > 0) { ` ` ` ` ` `// Get first element from ` ` ` `// queue ` ` ` `int` `x = q.front(); ` ` ` `q.pop(); ` ` ` ` ` `// Check if (x-1) is not ` ` ` `// encountered so far ` ` ` `if` `(!m[x - 1] && K > 0) { ` ` ` `// Update hash with ` ` ` `// this new element ` ` ` `m[x - 1] = 1; ` ` ` ` ` `// Insert (x-1) into ` ` ` `// queue ` ` ` `q.push(x - 1); ` ` ` ` ` `// Push (x-1) as ` ` ` `// new element ` ` ` `ans.push_back(x - 1); ` ` ` ` ` `// Decrement counter ` ` ` `// by 1 ` ` ` `K--; ` ` ` `} ` ` ` ` ` `// Check if (x+1) is not ` ` ` `// encountered so far ` ` ` `if` `(!m[x + 1] && K > 0) { ` ` ` `// Update hash with ` ` ` `// this new element ` ` ` `m[x + 1] = 1; ` ` ` ` ` `// Insert (x+1) into ` ` ` `// queue ` ` ` `q.push(x + 1); ` ` ` ` ` `// Push (x+1) as ` ` ` `// new element ` ` ` `ans.push_back(x + 1); ` ` ` ` ` `// Decrement counter ` ` ` `// by 1 ` ` ` `K--; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print result array ` ` ` `for` `(` `auto` `i : ans) ` ` ` `cout << i << ` `" "` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `A[] = { -1, 4, 6 }; ` ` ` `int` `K = 3; ` ` ` `int` `n = ` `sizeof` `(A) / ` `sizeof` `(A[0]); ` ` ` ` ` `minDistancePoints(A, K, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

-2 0 3

* Time Complexity: O(M*log(M))*, where M = N + K.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Pick points from array such that minimum distance is maximized
- Number of Integral Points between Two Points
- Find the maximum possible distance from origin using given points
- Find points at a given distance on a line of given slope
- Haversine formula to find distance between two points on a sphere
- Find the integer points (x, y) with Manhattan distance atleast N
- Ways to choose three points with distance between the most distant points <= L
- Count Integral points inside a Triangle
- Find the minimum distance between two numbers
- Minimum number of points to be removed to get remaining points on one side of axis
- Find the minimum sum of distance to A and B from any integer point in a ring of size N
- Binary Array Range Queries to find the minimum distance between two Zeros
- Program to calculate distance between two points
- Program for distance between two points on earth
- Program to calculate distance between two points in 3 D
- Hammered distance between N points in a 2-D plane
- Check whether it is possible to join two points given on circle such that distance between them is k
- Distance between end points of Hour and minute hand at given time
- Sort an Array of Points by their distance from a reference Point
- Minimum product of k integers in an array of positive Integers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.