Minimum time to reach given points on X-axis
Last Updated :
27 Jul, 2023
Given an array pos[] that represents N points on the X axis, a variable cur that represents the current position, and an array time. Where time[i] denotes the time taken to cover 1 unit of distance toward ith point, the task is to reach at any of the given points in minimum time and print the minimum time taken.
Examples:
Input: N = 3, cur = 4, pos = {1, 5, 6}, time = {2, 3, 1}
Output: 2
Explanation: We will go position 6 which will take 1 unit time to cover 1 unit distance and the distance between current position and 6 is 2 so answer will be 2.
Input: N = 2, cur = 1, pos = {1, 6}, time = {10, 3}
Output: 0
Explanation: We are already at point 1.
Approach: To solve the problem follow the below idea:
- Calculate the distance between each point by distance = abs(cur-pos[i]) where pos[i] is ith point. So after finding it calculate the time to reach at that point distance*time[i].
- Traverse all the point and find out the distance between the point and current position.
- Multiply the distance with time to cover 1 unit toward that point.
- Find the smallest from all.
Follow the steps to solve the problem:
- Initialize a variable say ans = INT_MAX to store the minimum time required to reach any of the given points.
- Iterate the points[] array and for each point, calculate the distance between the current position and the point, distance = abs(cur – pos[i]).
- Multiply the distance with the corresponding time required to reach that point from the time array.
- Update the ans variable with the minimum of its current value and the calculated value.
- Return ans.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumTime( int N, int cur, vector< int >& pos,
vector< int >& time )
{
int ans = INT_MAX;
for ( int i = 0; i < N; i++) {
int distance = abs (cur - pos[i]);
ans = min(ans, distance * time [i]);
}
return ans;
}
int main()
{
int N = 3, cur = 4;
vector< int > pos = { 1, 5, 6 };
vector< int > time = { 2, 3, 1 };
cout << minimumTime(N, cur, pos, time );
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int minimumTime( int N, int cur, ArrayList<Integer> pos,
ArrayList<Integer> time) {
int ans = Integer.MAX_VALUE;
for ( int i = 0 ; i < N; i++) {
int distance = Math.abs(cur - pos.get(i));
ans = Math.min(ans, distance * time.get(i));
}
return ans;
}
public static void main(String[] args) {
int N = 3 , cur = 4 ;
ArrayList<Integer> pos = new ArrayList<Integer>();
pos.add( 1 );
pos.add( 5 );
pos.add( 6 );
ArrayList<Integer> time = new ArrayList<Integer>();
time.add( 2 );
time.add( 3 );
time.add( 1 );
System.out.println(minimumTime(N, cur, pos, time));
}
}
|
Python3
def minimumTime(N, cur, pos, time):
ans = float ( 'inf' )
for i in range (N):
distance = abs (cur - pos[i])
ans = min (ans, distance * time[i])
return ans
if __name__ = = '__main__' :
N = 3
cur = 4
pos = [ 1 , 5 , 6 ]
time = [ 2 , 3 , 1 ]
print (minimumTime(N, cur, pos, time))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static int MinimumTime( int N, int cur, List< int > pos,
List< int > time)
{
int ans = int .MaxValue;
for ( int i = 0; i < N; i++)
{
int distance = Math.Abs(cur - pos[i]);
ans = Math.Min(ans, distance * time[i]);
}
return ans;
}
public static void Main( string [] args)
{
int N = 3, cur = 4;
List< int > pos = new List< int > { 1, 5, 6 };
List< int > time = new List< int > { 2, 3, 1 };
Console.WriteLine(MinimumTime(N, cur, pos, time));
}
}
|
Javascript
function minimumTime(N, cur, pos, time) {
let ans = Infinity;
for (let i = 0; i < N; i++) {
let distance = Math.abs(cur - pos[i]);
ans = Math.min(ans, distance * time[i]);
}
return ans;
}
const N = 3;
const cur = 4;
const pos = [1, 5, 6];
const time = [2, 3, 1];
console.log(minimumTime(N, cur, pos, time));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...