Minimum absolute value of (K – arr[i]) for all possible values of K over the range [0, N – 1]
Given a positive integer N and a sorted array arr[] consisting of M integers, the task is to find the minimum absolute value of (K – arr[i]) for all possible values of K over the range [0, N – 1].
Examples:
Input: N = 5, arr[] = {0, 4}
Output: 0 1 2 1 0
Explanation:
Below are the possible minimum value for all possible values of K over the range [0, N – 1]:
- K = 0: The minimum value of abs(K – arr[i]) is obtained by considering arr[i] as 0. Therefore, the value is abs(0 – 0) = 0.
- K = 1: The minimum value of abs(K – arr[i]) is obtained by considering arr[i] as 0. Therefore, the value is abs(1 – 0) = 1.
- K = 2: The minimum value of abs(K – arr[i]) is obtained by considering arr[i] as 0. Therefore, the value is abs(2 – 0) = 2.
- K = 3: The minimum value of abs(K – arr[i]) is obtained by considering arr[i] as 4. Therefore, the value is abs(3 – 4) = 1.
- K = 4: The minimum value of abs(K – arr[i]) is obtained by considering arr[i] as 4. Therefore, the value is abs(4 – 4) = 0.
Input: N = 6, arr[] = {0, 1, 4, 5}
Output: 0 0 1 1 0 0
Approach: The given problem can be solved by choosing the value from the array which is just greater or smaller than the current value of K. Follow the steps below to solve the problem:
- Initialize a variable, say ind as 0, and a variable, say prev to arr[0] that stores the previously assigned value.
- Iterate over the range [0, N – 1] using the variable K and perform the following steps:
- Initialize a variable, say distance to store the minimum absolute value of (K – arr[i]).
- If the value of i is less than arr[0], then update the value of the distance to (arr[0] – i).
- Otherwise, if the value of i is at least prev, the value of (ind + 1) is less than the M and the value of i is at most arr[ind + 1], then perform the following steps:
- Update the value of distance to the minimum of (i – prev) and (arr[ind + 1] – i).
- If the value of i is equal to arr[ind + 1], then update the value of distance to 0, prev to arr[ind + 1], and increment the value of ind by 1.
- If the value of i is greater than prev, then update the value of distance to the (i – prev).
- After completing the above steps, print the value of distance as the minimum absolute value of (K – arr[i]) for the current value of K.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minimumDistance(vector< int > arr,
int N)
{
int ind = 0;
int prev = arr[ind];
int s = arr.size();
for ( int i = 0; i < N; i++) {
int distance = INT_MAX;
if (i < arr[0]) {
distance = arr[0] - i;
}
else if (i >= prev && ind + 1 < s
&& i <= arr[ind + 1]) {
distance = min(i - prev,
arr[ind + 1] - i);
if (i == arr[ind + 1]) {
distance = 0;
prev = arr[ind + 1];
ind++;
}
}
else {
distance = i - prev;
}
cout << distance << " " ;
}
}
int main()
{
int N = 5;
vector< int > arr = { 0, 4 };
minimumDistance(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static void minimumDistance( int arr[],
int N)
{
int ind = 0 ;
int prev = arr[ind];
int s = arr.length;
for ( int i = 0 ; i < N; i++) {
int distance = Integer.MAX_VALUE;
if (i < arr[ 0 ]) {
distance = arr[ 0 ] - i;
}
else if (i >= prev && ind + 1 < s
&& i <= arr[ind + 1 ]) {
distance = Math.min(i - prev,
arr[ind + 1 ] - i);
if (i == arr[ind + 1 ]) {
distance = 0 ;
prev = arr[ind + 1 ];
ind++;
}
}
else {
distance = i - prev;
}
System.out.print(distance+ " " );
}
}
public static void main (String[] args) {
int N = 5 ;
int arr[] = { 0 , 4 };
minimumDistance(arr, N);
}
}
|
Python3
def minimumDistance(arr, N):
ind = 0 ;
prev = arr[ind];
s = len (arr);
for i in range (N):
distance = 10 * * 9 ;
if (i < arr[ 0 ]):
distance = arr[ 0 ] - i;
elif (i > = prev and ind + 1 < s
and i < = arr[ind + 1 ]):
distance = min (i - prev, arr[ind + 1 ] - i);
if (i = = arr[ind + 1 ]):
distance = 0 ;
prev = arr[ind + 1 ];
ind + = 1 ;
else :
distance = i - prev;
print (distance, end = " " );
N = 5 ;
arr = [ 0 , 4 ];
minimumDistance(arr, N);
|
C#
using System;
class GFG
{
public static void minimumDistance( int []arr,
int N)
{
int ind = 0;
int prev = arr[ind];
int s = arr.Length;
for ( int i = 0; i < N; i++) {
int distance = Int32.MaxValue;
if (i < arr[0]) {
distance = arr[0] - i;
}
else if (i >= prev && ind + 1 < s
&& i <= arr[ind + 1]) {
distance = Math.Min(i - prev,
arr[ind + 1] - i);
if (i == arr[ind + 1]) {
distance = 0;
prev = arr[ind + 1];
ind++;
}
}
else {
distance = i - prev;
}
Console.Write(distance+ " " );
}
}
public static void Main ( string [] args) {
int N = 5;
int []arr = { 0, 4 };
minimumDistance(arr, N);
}
}
|
Javascript
<script>
function minimumDistance(arr, N) {
let ind = 0;
let prev = arr[ind];
let s = arr.length;
for (let i = 0; i < N; i++) {
let distance = Number.MAX_SAFE_INTEGER;
if (i < arr[0]) {
distance = arr[0] - i;
}
else if (i >= prev && ind + 1 < s
&& i <= arr[ind + 1]) {
distance = Math.min(i - prev,
arr[ind + 1] - i);
if (i == arr[ind + 1]) {
distance = 0;
prev = arr[ind + 1];
ind++;
}
}
else {
distance = i - prev;
}
document.write(distance + " " );
}
}
let N = 5;
let arr = [0, 4];
minimumDistance(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
14 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...