Given an integer array A[] of length N and an integer K. The task is to find K distinct integral points that are not present in the given array such that the sum of their distances from the nearest point in A[] is minimized.
An integral point is 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++
#include <bits/stdc++.h>
using namespace std;
void minDistancePoints( int A[],
int K,
int n)
{
map< int , int > m;
queue< int > q;
for ( int i = 0; i < n; ++i) {
m[A[i]] = 1;
q.push(A[i]);
}
vector< int > ans;
while (K > 0) {
int x = q.front();
q.pop();
if (!m[x - 1] && K > 0) {
m[x - 1] = 1;
q.push(x - 1);
ans.push_back(x - 1);
K--;
}
if (!m[x + 1] && K > 0) {
m[x + 1] = 1;
q.push(x + 1);
ans.push_back(x + 1);
K--;
}
}
for ( auto i : ans)
cout << i << " " ;
}
int main()
{
int A[] = { -1, 4, 6 };
int K = 3;
int n = sizeof (A) / sizeof (A[0]);
minDistancePoints(A, K, n);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
class Geeks{
static void minDistancePoints( int A[],
int K, int n)
{
Map<Integer,
Boolean> m = new HashMap<Integer,
Boolean>();
Queue<Integer> q = new LinkedList<Integer>();
for ( int i = 0 ; i < n; ++i)
{
m.put(A[i], true );
q.add(A[i]);
}
LinkedList<Integer> ans = new LinkedList<Integer>();
while (K > 0 )
{
int x = q.poll();
if (!m.containsKey(x - 1 ) && K > 0 )
{
m.put(x - 1 , true );
q.add(x - 1 );
ans.add(x - 1 );
K--;
}
if (!m.containsKey(x + 1 ) && K > 0 )
{
m.put(x + 1 , true );
q.add(x + 1 );
ans.add(x + 1 );
K--;
}
}
for (Integer i : ans)
System.out.print(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);
}
}
|
Python3
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)
|
C#
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);
}
}
|
Javascript
<script>
function minDistancePoints(A, K, n)
{
let m = new Map();
let q = [];
for (let i = 0; i < n; ++i)
{
m.set(A[i], true );
q.push(A[i]);
}
let ans = [];
while (K > 0)
{
let x = q.shift();
if (!m.has(x - 1) && K > 0)
{
m.set(x - 1, true );
q.push(x - 1);
ans.push(x - 1);
K--;
}
if (!m.has(x + 1) && K > 0)
{
m.set(x + 1, true );
q.push(x + 1);
ans.push(x + 1);
K--;
}
}
for (let i = 0; i < ans.length; i++)
document.write(ans[i] + " " );
}
let A = [ -1, 4, 6 ];
let K = 3;
let n = A.length;
minDistancePoints(A, K, n);
</script>
|
Time Complexity: O(M*log(M)), where M = N + K.
Auxiliary Space: O(N)