Minimum distance to visit given K points on X-axis after starting from the origin
Given a sorted array, arr[] of size N representing the positions of the ith point on X-axis and an integer K, the task is to find the minimum distance required to travel to visit K point starting from the origin of X-axis.
Examples :
Input: arr[]={-30, -10, 10, 20, 50}, K = 3
Output: 40
Explanation:
Moving from origin to the second point. Therefore, the total distance traveled = 10.
Moving from the second point to the third point. Therefore, total traveled = 10 + 10 = 20
Moving from the third point to the fourth point. Therefore, total distance traveled = 20 + 20 = 40
Therefore, the total distance travelled to visit K point is 40.
Input: arr[]={-1, -5, -4, -3, 6}, K = 3
Output: 6
Approach: The problem can be solved by visiting each K consecutive point and printing the minimum distance travelled to visit K consecutive points. Follow the steps below to solve the problem:
- Initialize a variable, say res, to store the minimum distance travelled to visit K point.
- Initialize a variable, say dist, to store the distance travelled to visit K points.
- Traverse the array and check the following conditions.
- If the value of (arr[i] >= 0 and arr[i + K -1] >= 0) is true then update dist = max(arr[i], arr[i + K -1]).
- Otherwise, dist = abs(arr[i]) + abs(arr[i + K – 1]) + min(abs(arr[i]), abs(arr[i + K – 1])).
- Finally, update res = min(res, dist).
- Finally, print the value of res.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MinDistK( int arr[], int N, int K)
{
int res = INT_MAX;
int dist = 0;
for ( int i = 0; i <= (N - K); i++) {
if (arr[i] >= 0 &&
arr[i + K - 1] >= 0) {
dist = max(arr[i], arr[i + K - 1]);
}
else {
dist = abs (arr[i]) +
abs (arr[i + K - 1])
+ min( abs (arr[i]),
abs (arr[i + K - 1]));
}
res = min(res, dist);
}
return res;
}
int main()
{
int K = 3;
int arr[] = { -30, -10, 10, 20, 50 };
int N = sizeof (arr) / sizeof (arr[0]);
cout<< MinDistK(arr, N, K);
}
|
Java
import java.util.*;
class solution{
static int MinDistK( int arr[],
int N, int K)
{
int res = Integer.MAX_VALUE;
int dist = 0 ;
for ( int i = 0 ;
i <= (N - K); i++)
{
if (arr[i] >= 0 &&
arr[i + K - 1 ] >= 0 )
{
dist = Math.max(arr[i],
arr[i + K - 1 ]);
}
else
{
dist = Math.abs(arr[i]) +
Math.abs(arr[i + K - 1 ]) +
Math.min(Math.abs(arr[i]),
Math.abs(arr[i + K - 1 ]));
}
res = Math.min(res, dist);
}
return res;
}
public static void main(String args[])
{
int K = 3 ;
int arr[] = {- 30 , - 10 ,
10 , 20 , 50 };
int N = arr.length;
System.out.println(MinDistK(arr, N, K));
}
}
|
Python3
import sys
def MinDistK(arr, N, K):
res = sys.maxsize
dist = 0
for i in range (N - K + 1 ):
if (arr[i] > = 0 and arr[i + K - 1 ] > = 0 ):
dist = max (arr[i], arr[i + K - 1 ])
else :
dist = ( abs (arr[i]) + abs (arr[i + K - 1 ]) +
min ( abs (arr[i]), abs (arr[i + K - 1 ])))
res = min (res, dist)
return res
if __name__ = = '__main__' :
K = 3
arr = [ - 30 , - 10 , 10 , 20 , 50 ]
N = len (arr)
print (MinDistK(arr, N, K))
|
C#
using System;
class GFG{
static int MinDistK( int [] arr,
int N, int K)
{
int res = Int32.MaxValue;
int dist = 0;
for ( int i = 0; i <= (N - K); i++)
{
if (arr[i] >= 0 &&
arr[i + K - 1] >= 0)
{
dist = Math.Max(arr[i],
arr[i + K - 1]);
}
else
{
dist = Math.Abs(arr[i]) +
Math.Abs(arr[i + K - 1]) +
Math.Min(Math.Abs(arr[i]),
Math.Abs(arr[i + K - 1]));
}
res = Math.Min(res, dist);
}
return res;
}
public static void Main()
{
int K = 3;
int [] arr = { -30, -10, 10, 20, 50};
int N = arr.Length;
Console.WriteLine(MinDistK(arr, N, K));
}
}
|
Javascript
<script>
function MinDistK(arr, N, K)
{
let res = Number.MAX_VALUE;
let dist = 0;
for (let i = 0;
i <= (N - K); i++)
{
if (arr[i] >= 0 &&
arr[i + K - 1] >= 0)
{
dist = Math.max(arr[i],
arr[i + K - 1]);
}
else
{
dist = Math.abs(arr[i]) +
Math.abs(arr[i + K - 1]) +
Math.min(Math.abs(arr[i]),
Math.abs(arr[i + K - 1]));
}
res = Math.min(res, dist);
}
return res;
}
let K = 3;
let arr = [-30, -10,
10, 20, 50];
let N = arr.length;
document.write(MinDistK(arr, N, K));
</script>
|
Time Complexity: O(N-K), Where n is the length of the given array for the given K value.
Auxiliary Space: O(1)
Last Updated :
29 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...