GeeksforGeeks App
Open App
Browser
Continue

# Check if it is possible to travel all points in given time by moving in adjacent four directions

Given 3 arrays X[], Y[], and T[] all of the size N where X[i] and Y[i] represent the i-th coordinate and T[i] represents the time in seconds. Find it is possible to reach all the coordinates (X[i], Y[i]) in time T[i] from starting coordinates (0, 0). The pointer can move in four directions ( x +1, y ), ( x − 1, y ), ( x, y + 1) and (x, y − 1). From going from one coordinate to another takes 1 second and cannot stay at own palace.

Examples:

Input: N = 2, X[] = {1, 1},
Y[] = {2, 1},
T[] = {3, 6}
Output: Yes
Explanation: Suppose 2D Matrix like this:

In above matrix each point is defined as x and y coordinate Travel from ( 0, 0 ) -> ( 0, 1 ) -> ( 1, 1 ) -> ( 1, 2 ) in 3 seconds Then from ( 1, 2 ) -> ( 1, 1 ) -> ( 1, 0 ) -> ( 1, 1 ) on 6th second. So, yes it is possible to reach all the coordinates in given time.

Input: N = 1, X[] = {100 },
Y[] = {100 },
T[] = {2}
Output: No
Explanation: It is not possible to reach coordinates X and Y in 2 seconds from coordinates ( 0, 0 ).

Approach: The idea to solve this problem is based on the fact that to move from i-th point to (i+1)-th point it takes abs(X[i+1] – X[i]) + abs(Y[i+1] – Y[i]) time. In the case of the first point, the previous point is (0, 0). So, if this time is less than T[i] then it’s fine otherwise, it violates the condition. Follow the steps below to solve the problem:

• Make three variables currentX, currentY, currentTime and initialize to zero.
• Make a bool variable isPossible and initialize to true.
• Iterate over the range [0, N) using the variable i and perform the following tasks:
• If (abs(X[i] – currentX ) + abs( Y[i] – currentY )) is greater than (T[i] – currentTime) then make isPossible false.
• Else if,  ((abs(X[i] – currentX ) + abs( Y[i] – currentY )) % 2 is not equal to (T[i] – currentTime) % 2 then make isPossible false.
• Else change the previous values of currentX, currentY and currentTime with Xi, Yi and Ti.
• After performing the above steps, return the value of isPossible as the answer.

Below is the implementation of the above approach.

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to check if it is possible``// to traverse all the points.``bool` `CheckItisPossible(``int` `X[], ``int` `Y[],``                       ``int` `T[], ``int` `N)``{` `    ``// Make 3 variables to store given``    ``// ith values``    ``int` `currentX = 0, currentY = 0,``        ``currentTime = 0;` `    ``// Also, make a bool variable to``    ``// check it is possible``    ``bool` `IsPossible = ``true``;` `    ``// Now, iterate on all the coordinates``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// check first condition``        ``if` `((``abs``(X[i] - currentX)``             ``+ ``abs``(Y[i] - currentY))``            ``> (T[i] - currentTime)) {``            ``// means thats not possible to``            ``// reach current coordinate``            ``// at Ithtime from previous coordinate``            ``IsPossible = ``false``;``            ``break``;``        ``}``        ``else` `if` `(((``abs``(X[i] - currentX)``                   ``+ ``abs``(Y[i] - currentY))``                  ``% 2)``                 ``> ((T[i] - currentTime) % 2)) {``            ``// means thats not possible to``            ``// reach current coordinate``            ``// at Ithtime from previous coordinate``            ``IsPossible = ``false``;``            ``break``;``        ``}``        ``else` `{``            ``// If both above conditions are false``            ``// then we change the values of current``            ``// coordinates``            ``currentX = X[i];``            ``currentY = Y[i];``            ``currentTime = T[i];``        ``}``    ``}` `    ``return` `IsPossible;``}` `// Driver Code``int` `main()``{``    ``int` `X[] = { 1, 1 };``    ``int` `Y[] = { 2, 1 };``    ``int` `T[] = { 3, 6 };``    ``int` `N = ``sizeof``(X[0]) / ``sizeof``(``int``);``    ``bool` `ans = CheckItisPossible(X, Y, T, N);` `    ``if` `(ans == ``true``) {``        ``cout << ``"Yes"``             ``<< ``"\n"``;``    ``}``    ``else` `{``        ``cout << ``"No"``             ``<< ``"\n"``;``    ``}``    ``return` `0;``}`

## Java

 `// Java program for the above approach``public` `class` `GFG {``    ` `    ``// Function to check if it is possible``    ``// to traverse all the points.``    ``static` `boolean` `CheckItisPossible(``int` `X[], ``int` `Y[],``                        ``int` `T[], ``int` `N)``    ``{``    ` `        ``// Make 3 variables to store given``        ``// ith values``        ``int` `currentX = ``0``, currentY = ``0``,``            ``currentTime = ``0``;``    ` `        ``// Also, make a bool variable to``        ``// check it is possible``        ``boolean` `IsPossible = ``true``;``    ` `        ``// Now, iterate on all the coordinates``        ``for` `(``int` `i = ``0``; i < N; i++) {``    ` `            ``// check first condition``            ``if` `((Math.abs(X[i] - currentX) +``                 ``Math.abs(Y[i] - currentY)) > (T[i] - currentTime)) {``                ` `                ``// means thats not possible to``                ``// reach current coordinate``                ``// at Ithtime from previous coordinate``                ``IsPossible = ``false``;``                ``break``;``            ``}``            ``else` `if` `(((Math.abs(X[i] - currentX) +``                       ``Math.abs(Y[i] - currentY)) % ``2``) > ((T[i] - currentTime) % ``2``)) {``                ``// means thats not possible to``                ``// reach current coordinate``                ``// at Ithtime from previous coordinate``                ``IsPossible = ``false``;``                ``break``;``            ``}``            ``else` `{``                ``// If both above conditions are false``                ``// then we change the values of current``                ``// coordinates``                ``currentX = X[i];``                ``currentY = Y[i];``                ``currentTime = T[i];``            ``}``        ``}``    ` `        ``return` `IsPossible;``    ``}``    ` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `X[] = { ``1``, ``1` `};``        ``int` `Y[] = { ``2``, ``1` `};``        ``int` `T[] = { ``3``, ``6` `};``        ``int` `N = X.length;``        ``boolean` `ans = CheckItisPossible(X, Y, T, N);``    ` `        ``if` `(ans == ``true``) {``            ``System.out.println(``"Yes"``);``        ``}``        ``else` `{``            ``System.out.println(``"No"``);``        ``}``    ``}``}` `// This code is contributed by AnkThon`

## Python3

 `# python program for the above approach` `# Function to check if it is possible``# to traverse all the points.``def` `CheckItisPossible(X, Y, T, N):` `        ``# Make 3 variables to store given``        ``# ith values``    ``currentX ``=` `0``    ``currentY ``=` `0``    ``currentTime ``=` `0` `    ``# Also, make a bool variable to``    ``# check it is possible``    ``IsPossible ``=` `True` `    ``# Now, iterate on all the coordinates``    ``for` `i ``in` `range``(``0``, N):` `                ``# check first condition``        ``if` `((``abs``(X[i] ``-` `currentX)``             ``+` `abs``(Y[i] ``-` `currentY))``                ``> (T[i] ``-` `currentTime)):``             ``# means thats not possible to``             ``# reach current coordinate``             ``# at Ithtime from previous coordinate``            ``IsPossible ``=` `False``            ``break` `        ``elif` `(((``abs``(X[i] ``-` `currentX)``                ``+` `abs``(Y[i] ``-` `currentY))``               ``%` `2``)``              ``> ((T[i] ``-` `currentTime) ``%` `2``)):``            ``# means thats not possible to``            ``# reach current coordinate``            ``# at Ithtime from previous coordinate``            ``IsPossible ``=` `False``            ``break` `        ``else``:``           ``# If both above conditions are false``           ``# then we change the values of current``           ``# coordinates``            ``currentX ``=` `X[i]``            ``currentY ``=` `Y[i]``            ``currentTime ``=` `T[i]` `    ``return` `IsPossible` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``X ``=` `[``1``, ``1``]``    ``Y ``=` `[``2``, ``1``]``    ``T ``=` `[``3``, ``6``]``    ``N ``=` `len``(X)``    ``ans ``=` `CheckItisPossible(X, Y, T, N)` `    ``if` `(ans ``=``=` `True``):``        ``print``(``"Yes"``)``    ``else``:``        ``print``(``"No"``)` `    ``# This code is contributed by rakeshsahni`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG {``    ` `    ``// Function to check if it is possible``    ``// to traverse all the points.``    ``static` `bool` `CheckItisPossible(``int` `[]X, ``int` `[]Y,``                        ``int` `[]T, ``int` `N)``    ``{``    ` `        ``// Make 3 variables to store given``        ``// ith values``        ``int` `currentX = 0, currentY = 0,``            ``currentTime = 0;``    ` `        ``// Also, make a bool variable to``        ``// check it is possible``        ``bool` `IsPossible = ``true``;``    ` `        ``// Now, iterate on all the coordinates``        ``for` `(``int` `i = 0; i < N; i++) {``    ` `            ``// check first condition``            ``if` `((Math.Abs(X[i] - currentX) +``                 ``Math.Abs(Y[i] - currentY)) > (T[i] - currentTime)) {``                ` `                ``// means thats not possible to``                ``// reach current coordinate``                ``// at Ithtime from previous coordinate``                ``IsPossible = ``false``;``                ``break``;``            ``}``            ``else` `if` `(((Math.Abs(X[i] - currentX) +``                       ``Math.Abs(Y[i] - currentY)) % 2) > ((T[i] - currentTime) % 2)) {``                ``// means thats not possible to``                ``// reach current coordinate``                ``// at Ithtime from previous coordinate``                ``IsPossible = ``false``;``                ``break``;``            ``}``            ``else` `{``                ``// If both above conditions are false``                ``// then we change the values of current``                ``// coordinates``                ``currentX = X[i];``                ``currentY = Y[i];``                ``currentTime = T[i];``            ``}``        ``}``    ` `        ``return` `IsPossible;``    ``}``    ` `    ``// Driver Code``    ``public` `static` `void` `Main()``    ``{``        ``int` `[]X = { 1, 1 };``        ``int` `[]Y = { 2, 1 };``        ``int` `[]T = { 3, 6 };``        ``int` `N = X.Length;``        ``bool` `ans = CheckItisPossible(X, Y, T, N);``    ` `        ``if` `(ans == ``true``) {``            ``Console.Write(``"Yes"``);``        ``}``        ``else` `{``            ``Console.Write(``"No"``);``        ``}``    ``}``}` `// This code is contributed by Samim Hossain Mondal.`

## Javascript

 ``

Output

`Yes`

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

Approach 2:

• Firstly, the code initializes the values of coordinates x, y and time t. It then calculates the total time required to reach the last coordinate t[n-1] – t[0].
• Next, the code iterates over all the coordinates except the first and the last ones. It calculates the minimum time required to reach the current coordinate using two values – the time taken to reach the current coordinate directly from the previous coordinate and the time difference between the current and previous times. The min() function is used to choose the smaller value between these two.
• Finally, the code checks if the minimum time required to reach all the coordinates is less than or equal to the total time given. If it is, then it outputs “Yes”, otherwise “No”.

Here is the code of above approach:

## C++

 `#include ``using` `namespace` `std;` `int` `main() {``    ``int` `n = 2;``  ``int` `x[n] = { 1, 1 };``    ``int` `y[n] = { 2, 1 };``    ``int` `t[n] = { 3, 6 };`` ` `    ``int` `total_time = t[n-1] - t[0];``    ``int` `min_time = ``abs``(x[1]-x[0]) + ``abs``(y[1]-y[0]);``    ``for` `(``int` `i = 1; i < n-1; i++) {``        ``min_time += min(``abs``(x[i]-x[i-1])+``abs``(y[i]-y[i-1]), t[i]-t[i-1]);``    ``}``    ``if` `(min_time <= total_time) {``        ``cout << ``"Yes"` `<< endl;``    ``}``    ``else` `{``        ``cout << ``"No"` `<< endl;``    ``}``    ``return` `0;``}`

## Python3

 `# Declare and initialize variables``n ``=` `2``x ``=` `[``1``, ``1``]``y ``=` `[``2``, ``1``]``t ``=` `[``3``, ``6``]` `# Calculate the total time taken to reach all the points``total_time ``=` `t[n``-``1``] ``-` `t[``0``]` `# Calculate the minimum time required to reach all the points``# starting from the first point and following the given order``min_time ``=` `abs``(x[``1``]``-``x[``0``]) ``+` `abs``(y[``1``]``-``y[``0``])` `# Loop through the remaining points to calculate the minimum time``for` `i ``in` `range``(``1``, n``-``1``):``    ``time_to_reach_current_point ``=` `abs``(x[i]``-``x[i``-``1``]) ``+` `abs``(y[i]``-``y[i``-``1``])``    ``wait_time ``=` `t[i] ``-` `t[i``-``1``] ``-` `time_to_reach_current_point``    ``wait_time ``=` `wait_time ``if` `wait_time > ``0` `else` `0``    ``min_time ``+``=` `min``(time_to_reach_current_point, wait_time)` `# Check if the minimum time required is less than or equal to the total``# time available``if` `min_time <``=` `total_time:``    ``print``(``"Yes"``)``else``:``    ``print``(``"No"``)`

## Javascript

 `// Declare and initialize variables``let n = 2;     ``let x = [1, 1];  ``let y = [2, 1];  ``let t = [3, 6];  ` `// Calculate the total time taken to reach all the points``let total_time = t[n-1] - t[0];` `// Calculate the minimum time required to reach all the points``// starting from the first point and following the given order``let min_time = Math.abs(x[1]-x[0]) + Math.abs(y[1]-y[0]);` `// Loop through the remaining points to calculate the minimum time``for` `(let i = 1; i < n-1; i++) {``    ``let time_to_reach_current_point = Math.abs(x[i]-x[i-1]) + Math.abs(y[i]-y[i-1]);``    ``let wait_time = t[i] - t[i-1] - time_to_reach_current_point;``    ``wait_time = wait_time > 0 ? wait_time : 0;``    ``min_time += Math.min(time_to_reach_current_point, wait_time);``}` `// Check if the minimum time required is less than or equal to the total time available``if` `(min_time <= total_time) {``    ``console.log(``"Yes"``);``}``else` `{``    ``console.log(``"No"``);``}`

Output

`Yes`

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

My Personal Notes arrow_drop_up