Open In App

Minimum time to reach given points on X-axis

Last Updated : 27 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
 
// Function to find minimum time
int minimumTime(int N, int cur, vector<int>& pos,
                vector<int>& time)
{
 
    // Taking our answer as maximum
    int ans = INT_MAX;
 
    // Iterating over array
    for (int i = 0; i < N; i++) {
 
        // Finding distance between
        // current position ans point
        int distance = abs(cur - pos[i]);
 
        // taking minimum
        ans = min(ans, distance * time[i]);
    }
    return ans;
}
 
// Drivers code
int main()
{
 
    int N = 3, cur = 4;
 
    vector<int> pos = { 1, 5, 6 };
 
    vector<int> time = { 2, 3, 1 };
 
    // Function Call
    cout << minimumTime(N, cur, pos, time);
 
    return 0;
}


Java




import java.util.*;
 
public class GFG {
 
    // Function to find minimum time
    static int minimumTime(int N, int cur, ArrayList<Integer> pos,
                            ArrayList<Integer> time) {
 
        // Taking our answer as maximum
        int ans = Integer.MAX_VALUE;
 
        // Iterating over array
        for (int i = 0; i < N; i++) {
 
            // Finding distance between
            // current position and point
            int distance = Math.abs(cur - pos.get(i));
 
            // taking minimum
            ans = Math.min(ans, distance * time.get(i));
        }
        return ans;
    }
 
    // Drivers code
    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);
 
        // Function Call
        System.out.println(minimumTime(N, cur, pos, time));
    }
}
//contributed by tushar rokade


Python3




# Function to find minimum time
def minimumTime(N, cur, pos, time):
    # Taking our answer as maximum
    ans = float('inf')
 
    # Iterating over array
    for i in range(N):
        # Finding distance between
        # current position and point
        distance = abs(cur - pos[i])
 
        # taking minimum
        ans = min(ans, distance * time[i])
 
    return ans
 
# Driver code
if __name__ == '__main__':
    N = 3
    cur = 4
    pos = [1, 5, 6]
    time = [2, 3, 1]
     
    # Function call
    print(minimumTime(N, cur, pos, time))
     
#This code is contributed by Adi


C#




// C# code for the above approach:
using System;
using System.Collections.Generic;
 
public class GFG
{
    // Function to find minimum time
    static int MinimumTime(int N, int cur, List<int> pos,
                           List<int> time)
    {
        // Taking our answer as maximum
        int ans = int.MaxValue;
 
        // Iterating over list
        for (int i = 0; i < N; i++)
        {
            // Finding distance between
            // current position and point
            int distance = Math.Abs(cur - pos[i]);
 
            // taking minimum
            ans = Math.Min(ans, distance * time[i]);
        }
        return ans;
    }
 
    // Driver code
    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 };
 
        // Function Call
        Console.WriteLine(MinimumTime(N, cur, pos, time));
    }
}
 
// This code is contributed by Utkarsh Kumar


Javascript




// Javascript code for the above approach:
 
   // Function to find minimum time
function minimumTime(N, cur, pos, time) {
     
  // Taking our answer as maximum
  let ans = Infinity;
 
  // Iterating over array
  for (let i = 0; i < N; i++) {
   
// Finding distance between
// current position ans point
let distance = Math.abs(cur - pos[i]);
 
// taking minimum
ans = Math.min(ans, distance * time[i]);
  }
 
  return ans;
}
 
// Drivers code
const N = 3;
const cur = 4;
const pos = [1, 5, 6];
const time = [2, 3, 1];
 
// Function Call
console.log(minimumTime(N, cur, pos, time));
 
// This code is contributed by Pushpesh Raj.


Output

2




Time Complexity: O(N)
Auxiliary Space: O(1)



Similar Reads

Minimum number of points to be removed to get remaining points on one side of axis
We are given n points in a Cartesian plane. Our task is to find the minimum number of points that should be removed in order to get the remaining points on one side of any axis. Examples : Input : 4 1 1 2 2 -1 -1 -2 2 Output : 1 Explanation : If we remove (-1, -1) then all the remaining points are above x-axis. Thus the answer is 1. Input : 3 1 10
6 min read
Program to check if the points are parallel to X axis or Y axis
Given n points, we need to check if these n points are parallel to X axis or Y axis or to No axis. Examples: Input : x[] = {0, 0, 0, 0, 0| y[] = {9, 2, 1, 3, 4} Output : Parallel to Y Axis Input : x[] = {1, 2, 3| y[] = {9, 2, 1} Output : Not Parallel to X or Y Axis Approach To find the points parallel to X or Y axis just check if the points are sam
6 min read
Find the point on X-axis from given N points having least Sum of Distances from all other points
Given an array arr[] consisting of N integers, denoting N points lying on the X-axis, the task is to find the point which has the least sum of distances from all other points. Example: Input: arr[] = {4, 1, 5, 10, 2} Output: (4, 0) Explanation: Distance of 4 from rest of the elements = |4 - 1| + |4 - 5| + |4 - 10| + |4 - 2| = 12 Distance of 1 from
11 min read
Minimum swaps such that at least K points reach target within T time
Given N points moving along the X axis to the right whose initial position and speed are given in two arrays X[] (sorted in increasing order) and V[]. If at any time a point with a higher speed reaches a slow moving point then they will both continue moving with the slower speed. The task is to find out the minimum number of swaps we can perform on
7 min read
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 = 3Output: 40 Explanation: Moving from origin to the second point. Therefore,
6 min read
Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points
Given a 2D array rectangle[][] representing vertices of a rectangle {(0, 0), (L, 0), (0, B), (L, B)} of dimensions L * B, and another 2D-array, points[][] of size N in a Cartesian coordinate system. Draw a horizontal line parallel to the X-axis and a vertical line parallel to the Y-axis from each point of the given array. The task is to count all p
6 min read
Minimum time to reach a point with +t and -t moves at time t
Given a positive coordinate 'X' and you are at coordinate '0', the task is to find the minimum time required to get to coordinate 'X' with the following move : At time 't', you can either stay at the same position or take a jump of length exactly 't' either to the left or to the right. In other words, you can be at coordinate 'x - t', 'x' or 'x + t
9 min read
Maximize jump size to reach all given points from given starting point
Given an array, arr[] consisting of N integers representing coordinates on a number line and an integer S. Find the maximum size of jump required to visit all the coordinates at least once if the starting position is S. Also, Jumping in both directions is permitted. Example: Input: arr[]={1, 7, 3, 9, 5, 11}, S=3Output: 2Explanation: Jumps of size 2
5 min read
Count of Squares that are parallel to the coordinate axis from the given set of N points
Given an array of points points[] in a cartesian coordinate system, the task is to find the count of the squares that are parallel to the coordinate axis. Examples: Input:points[] = {(0, 0), (0, 2), (2, 0), (2, 2), (1, 1)} Output: 1 Explanation: As the points (0, 0), (0, 2), (2, 0), (2, 2) forms square which is parallel to the X-axis and Y-axis, He
13 min read
Number of lines from given N points not parallel to X or Y axis
Given N distinct integers points on 2D Plane. The task is to count the number of lines which are formed from given N points and not parallel to X or Y-axis. Examples: Input: points[][] = {{1, 2}, {1, 5}, {1, 15}, {2, 10}} Output: 3 Chosen pairs are {(1, 2), (2, 10)}, {(1, 5), (2, 10)}, {(1, 15), (2, 10)}. Input: points[][] = {{1, 2}, {2, 5}, {3, 15
9 min read
Article Tags :
Practice Tags :