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


In this problem, we will find the K points which are nearest to any of the given points from the points given in the array.

To find the points which are nearest to the given points, we can take the nums[p] + 1 or nums[p] -1 for each element of the array if it is not present in the array. If we require more points, we can take the nums[p] + 2 or nums[p] – 2 points, and so on.

Problem Statement

We have given a nums[] array containing the N positive and negative integers. Each point of the array is represented as a {nums[p], 0} in the two-dimensional space. We have also given the integer K. We need to find the total K points to minimize each point's distance from its nearest point, which is present in the nums[] array.

Sample Examples

Input

nums[] = {1, 3, 5}; K = 3

Output

0, 2, 4

Explanation

Here, 0 is nearest to 1. 2 and 4 is nearest to 3.

Input

nums[] = {2, 3, 5, 7, -10, 12}, K = 5;

Output

1, 4, 6, 8, -11

Explanation

  • 1 is nearest to 2.

  • 4 is nearest to 3.

  • 6 and 8 are nearest to 7.

  • -11 is nearest to -10.

Input

nums[] = {7, 8, 9, 10, 12, 11}; K = 6;

Output

6, 13, 5, 14, 4, 15

Explanation

When the nums[p] element is already present in the array, we need to take the nums[p] + 2 and so on.

Approach

In this approach, we will use the map to keep track of the original array elements and other nearest elements. Also, we will keep inserting the nearest element to the queue. After that, if we require more elements, we can take the nearest element to the previous nearest element to minimize the distance.

Algorithm

  • Step 1 − Define the 'num_map' map and 'que' queue. Also, define the 'res' list to store the K nearest elements.

  • Step 2 − Traverse through array elements and insert elements into the map and queue.

  • Step 3 − Make K iterations using the while loop.

  • Step 3.1 − Pop the first element from the queue, and store it in the 'temp' variable.

  • Step 3.2 − If temp -1 is not visited, and K is greater than 0, insert temp − 1 into the map and queue. Also, push temp − 1 into the res list and decrement the K by 1.

  • Step 3.3 − If temp + 1 is not visited, and K is greater than 0, insert the temp + 1 into the map, queue, and res list. Also, decrement the K by 1.

  • Step 4 − Traverse the 'res' list and print its value.

Example

#include <bits/stdc++.h>
using namespace std;

void printMinDistance(int nums[], int K, int n) {
   // Map to store visited points
   map<int, int> num_map;
   queue<int> que;
   // Insert array elements in the map and queue
   for (int p = 0; p < n; ++p) {
      num_map[nums[p]] = 1;
      que.push(nums[p]);
   }
   vector<int> res;
   // BFS algorithm
   while (K > 0) {
      // Pop the first element from the queue
      int temp = que.front();
      que.pop();
      // If temp - 1 is not visited yet
      if (!num_map[temp - 1] && K > 0) {
         num_map[temp - 1] = 1;
         que.push(temp - 1);
         res.push_back(temp - 1);
         K--;
      }
      // If temp + 1 is not visited
      if (!num_map[temp + 1] && K > 0) {
         num_map[temp + 1] = 1;
         que.push(temp + 1);
         res.push_back(temp + 1);
         K--;
      }
   }
   cout << "The K points are: ";
   for (auto p : res)
      cout << p << " ";
}
int main() {
   int nums[] = {2, 3, 5, 7, -10, 12};
   int K = 5;
   int n = sizeof(nums) / sizeof(nums[0]);
   printMinDistance(nums, K, n);
   return 0;
}

Output

The K points are: 1 4 6 8 -11
  • Time complexity − O(N + K), where N is the size of the array, and K is a number of elements we need to find.

  • Space complexity − O(N + K), to store elements into the map.

Conclusion

We found the K nearest elements to the array elements. Programmers may try to find the K nearest elements when elements are given in the pairs of {x, y}, and for that, you need to use the Euclidian distance.

Updated on: 25-Aug-2023

37 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements