Related Articles

# Check if it is possible to reach the point (X, Y) using distances given in an array

• Difficulty Level : Hard
• Last Updated : 16 Aug, 2021

Given an array arr[] consisting of N positive integers and two integers X and Y, the task is to check if it is possible to reach (X, Y) from (0, 0) such that moving to (Xf, Yf) from (Xi, Yi) is allowed only if the euclidean distance between them is present in the array arr[]. If it is possible, then print Yes. Otherwise, print No.

Note: Each distance present in the array arr[] can be used at most once.

Examples:

Input: arr[ ] = {2, 5}, X = 5, Y= 4
Output: Yes
Explanation:
Following are the moves required to reach from (0, 0) to (5, 4):

1. Move from point (0, 0) to (2, 0). The euclidian distance is sqrt((2 – 0)2 + (0 – 0)) = 2, which is present in the array.
2. Move from point (2, 0) to (5, 4). The euclidian distance is sqrt((5 – 2)2 + (4 – 0)2) = 5, which is present in the array.

After the above set of moves, the point (5, 4) can be reached. Hence, print Yes.

Input: arr[] = {4}, X = 3, Y= 4
Output: No

Approach: The given problem can be solved by using the following observations by considering the Euclidian Distance between points (0, 0) to (X, Y) as Z = sqrt(X*X + Y*Y), then the problem can be divided into 3 cases:

• If the sum of the array element is less than Z, it is impossible to reach (X, Y) by any set of moves.
• If the sum of the array element is equal to Z, it is possible to reach (X, Y) after N moves.
• Otherwise for every distance check for the following conditions:
• If for any A[i], A[i] > Z + (All other distance except A[i]) then it is never possible to reach (X, Y) because the path will be a polygon and for an N-polygon the sum of (N – 1) sides must be greater than the other side for every possible side.
• Otherwise, it is always possible to reach the point (X, Y).

From the above observations considering the three cases, print the result accordingly.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to check if the point (X, Y)``// is reachable from (0, 0) or not``int` `isPossibleToReach(``int` `A[], ``int` `N, ``int` `X, ``int` `Y)``{``    ``// Find the Euclidian Distance``    ``double` `distance = ``sqrt``(``double``(X * X + Y * Y));` `    ``// Calculate the maximum distance``    ``double` `mx = 0;``    ``for` `(``int` `i = 0; i < N; i++) {``        ``mx += ``double``(A[i]);``    ``}` `    ``// Case 1.``    ``if` `(mx < distance) {``        ``cout << ``"NO"``;``        ``return` `0;``    ``}` `    ``// Case 2.``    ``if` `((mx - distance) < 0.000001) {``        ``cout << ``"YES"``;``        ``return` `0;``    ``}` `    ``// Otherwise, check for the polygon``    ``// condition for each side``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``if` `(distance + mx``            ``< ``double``(2) * ``double``(A[i])) {``            ``cout << ``"No"``;``            ``return` `0;``        ``}``    ``}` `    ``// Otherwise, print Yes``    ``cout << ``"Yes"``;` `    ``return` `0;``}` `// Driver Code``int` `main()``{``    ``int` `A[] = { 2, 5 };``    ``int` `X = 5, Y = 4;``    ``int` `N = ``sizeof``(A) / ``sizeof``(A);` `    ``isPossibleToReach(A, N, X, Y);` `    ``return` `0;``}`

## Java

 `// C# program for the above approach``import` `java.io.*;` `class` `GFG{``    ` `// Function to check if the point (X, Y)``// is reachable from (0, 0) or not``static` `int` `isPossibleToReach(``int` `[]A, ``int` `N,``                             ``int` `X, ``int` `Y)``{``    ` `    ``// Find the Euclidian Distance``    ``double` `distance = Math.sqrt((X * X + Y * Y));``           ` `    ``// Calculate the maximum distance``    ``double` `mx = ``0``;``    ``for``(``int` `i = ``0``; i < N; i++)``    ``{``        ``mx += (A[i]);``    ``}` `    ``// Case 1.``    ``if` `(mx < distance)``    ``{``        ``System.out.print(``"NO"``);``        ``return` `0``;``    ``}` `    ``// Case 2.``    ``if` `((mx - distance) < ``0.000001``)``    ``{``        ``System.out.print(``"YES"``);``        ``return` `0``;``    ``}` `    ``// Otherwise, check for the polygon``    ``// condition for each side``    ``for``(``int` `i = ``0``; i < N; i++)``    ``{``        ``if` `(distance + mx < ``2` `* A[i])``        ``{``            ``System.out.print(``"No"``);``            ``return` `0``;``        ``}``    ``}` `    ``// Otherwise, print Yes``    ``System.out.print(``"Yes"``);` `    ``return` `0``;``}` `// Driver Code``public` `static` `void` `main (String[] args)``{``    ``int` `[]A = { ``2``, ``5` `};``    ``int` `X = ``5``, Y = ``4``;``    ``int` `N = A.length;``    ` `    ``isPossibleToReach(A, N, X, Y);``}``}` `// This code is contributed by shivanisinghss2110`

## Python3

 `# Python program for the above approach``import` `math` `# Function to check if the po(X, Y)``# is reachable from (0, 0) or not``def` `isPossibleToReach(A, N, X, Y):``  ` `    ``# Find the Euclidian Distance``    ``distance ``=` `math.sqrt(X ``*` `X ``+` `Y ``*` `Y)``    ` `    ``# Calculate the maximum distance``    ``mx ``=` `0``    ``for` `i ``in` `range``(N):``        ``mx ``+``=` `A[i]``        ` `    ``# Case 1.``    ``if` `(mx < distance):``        ``print``(``"NO"``)``        ``return` `0``    ` `    ``# Case 2.``    ``if` `((mx ``-` `distance) < ``0.000001``):``        ``print``(``"YES"``)``        ``return` `0``        ` `    ``# Otherwise, check for the polygon``    ``# condition for each side``    ``for` `i ``in` `range``(N):``        ``if` `(distance ``+` `mx < (``2``) ``*` `(A[i])):``            ``print``(``"No"``)``            ``return` `0``            ` `    ``# Otherwise, prYes``    ``print``(``"Yes"``)``    ``return` `0` `# Driver Code``A ``=` `[``2``, ``5``]``X ``=` `5``Y ``=` `4``N ``=` `len``(A)` `isPossibleToReach(A, N, X, Y)` `# This code is contributed by shivani.`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{``    ` `// Function to check if the point (X, Y)``// is reachable from (0, 0) or not``static` `int` `isPossibleToReach(``int` `[]A, ``int` `N,``                             ``int` `X, ``int` `Y)``{``    ` `    ``// Find the Euclidian Distance``    ``double` `distance = Math.Sqrt((X * X + Y * Y));``           ` `    ``// Calculate the maximum distance``    ``double` `mx = 0;``    ``for``(``int` `i = 0; i < N; i++)``    ``{``        ``mx += (A[i]);``    ``}` `    ``// Case 1.``    ``if` `(mx < distance)``    ``{``        ``Console.Write(``"NO"``);``        ``return` `0;``    ``}` `    ``// Case 2.``    ``if` `((mx - distance) < 0.000001)``    ``{``        ``Console.Write(``"YES"``);``        ``return` `0;``    ``}` `    ``// Otherwise, check for the polygon``    ``// condition for each side``    ``for``(``int` `i = 0; i < N; i++)``    ``{``        ``if` `(distance + mx < 2 * A[i])``        ``{``            ``Console.Write(``"No"``);``            ``return` `0;``        ``}``    ``}` `    ``// Otherwise, print Yes``    ``Console.Write(``"Yes"``);` `    ``return` `0;``}` `// Driver Code``static` `public` `void` `Main ()``{``    ``int` `[]A = { 2, 5 };``    ``int` `X = 5, Y = 4;``    ``int` `N = A.Length;``    ` `    ``isPossibleToReach(A, N, X, Y);``}``}` `// This code is contributed by shivanisinghss2110`

## Javascript

 ``
Output:
`Yes`

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up