Given **N** 2-Dimensional points. The task is to find the maximum possible distance from the origin using given points. Using the **i ^{th}** point

**(x**one can move from

_{i}, y_{i})**(a, b)**to

**(a + x**.

_{i}, b + y_{i})**Note:**

**N**lies between

**1**to

**1000**and each point can be used at most once.

**Examples:**

Input:arr[][] = {{1, 1}, {2, 2}, {3, 3}, {4, 4}}

Output:14.14

The farthest point we can move to is (10, 10).

Input:arr[][] = {{0, 10}, {5, -5}, {-5, -5}}

Output:10.00

**Approach:** The key observation is that when the points are ordered by the angles their vectors make with the x-axis, the answer will include vectors in some contiguous range. A proof of this fact can be read from here. Then, the solution is fairly easy to implement. Iterate over all possible ranges and compute the answers for each of them, taking the maximum as the result. When implemented appropriately, this is an O(N^{2}) approach.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the maximum possible ` `// distance from origin using given points. ` `void` `Max_Distance(vector<pair<` `int` `, ` `int` `> >& xy, ` `int` `n) ` `{ ` ` ` `// Sort the points with their tan angle ` ` ` `sort(xy.begin(), xy.end(), [](` `const` `pair<` `int` `, ` `int` `>& l, ` ` ` `const` `pair<` `int` `, ` `int` `>& r) { ` ` ` `return` `atan2l(l.second, l.first) ` ` ` `< atan2l(r.second, r.first); ` ` ` `}); ` ` ` ` ` `// Push the whole vector ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `xy.push_back(xy[i]); ` ` ` ` ` `// To store the required answer ` ` ` `int` `res = 0; ` ` ` ` ` `// Find the maximum possible answer ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `int` `x = 0, y = 0; ` ` ` `for` `(` `int` `j = i; j < i + n; j++) { ` ` ` `x += xy[j].first; ` ` ` `y += xy[j].second; ` ` ` `res = max(res, x * x + y * y); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print the required answer ` ` ` `cout << fixed << setprecision(2) << sqrtl(res); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `vector<pair<` `int` `, ` `int` `> > vec = { { 1, 1 }, ` ` ` `{ 2, 2 }, ` ` ` `{ 3, 3 }, ` ` ` `{ 4, 4 } }; ` ` ` ` ` `int` `n = vec.size(); ` ` ` ` ` `// Function call ` ` ` `Max_Distance(vec, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

14.14

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.

## Recommended Posts:

- Minimum distance to visit given K points on X-axis after starting from the origin
- Find the K closest points to origin using Priority Queue
- Ways to choose three points with distance between the most distant points <= L
- Find K Closest Points to the Origin
- Check whether it is possible to join two points given on circle such that distance between them is k
- Find integral points with minimum distance from given set of integers using BFS
- Find points at a given distance on a line of given slope
- Maximum distance between two points in coordinate plane using Rotating Caliper's Method
- Check if it is possible to reach (x, y) from origin in exactly Z steps using only plus movements
- Find the point on X-axis from given N points having least Sum of Distances from all other points
- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Haversine formula to find distance between two points on a sphere
- Find the integer points (x, y) with Manhattan distance atleast N
- Distance between end points of Hour and minute hand at given time
- Number of Integral Points between Two Points
- Prime points (Points that split a number into two primes)
- Minimum number of points to be removed to get remaining points on one side of axis
- Steps required to visit M points in order on a circular ring of N points
- Distance of chord from center when distance between center and another equal length chord is given
- Maximum Squares possible parallel to both axes from N distinct points

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.