# Minimum length of the shortest path of a triangle

Given **N** points on the plane, ( X_{1}, Y_{1} ), ( X_{2}, Y_{2} ), ( X_{3}, Y_{3} ), ……, ( X_{N}, Y_{N} ). The task is to calculate the minimum length of the shorter side of the triangle. and the path or points to place an isosceles triangle with any two sides of the triangle on the coordinate axis ( X axis and Y axis ) to cover all points.

**Note: ** A point is covered if it lies inside the triangle or on the side of the triangle.

**Examples:**

Input:(1, 3), (1, 1), (2, 1), (2, 2)

Output:Length -> 4 , Path -> ( 1, 4 ) and ( 4, 1 )

Input:(1, 2), (1, 1), (2, 1)

Output:Length -> 3 , Path -> ( 1, 3 ) and ( 3, 1 )

In the first example, the minimum length of the shortest path is equal to the maximum sum of the points, which is 1+3 or 2+2. So the path which will cover all the points is (1, 4) and (4, 1) on the coordinate axis.

**Below is the step by step algorithm to solve this problem:**

- Initialize ‘N’ points on a plane.
- Traverse through each point and find the sum of each point and store it in a variable ‘answer’.
- Replace the next maximum sum of the points with the previous sum.
- Then you will get the path on a coordinate axis ( 1, answer ) and ( answer, 1 ) which will cover all the points of isosceles triangle.

Below is the implementation of above algorithm:

## C++

`// C++ program to illustrate ` `// the above problem ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define ll long long ` ` ` `// function to get the minimum length of ` `// the shorter side of the triangle ` `void` `shortestLength(` `int` `n, ` `int` `x[], ` `int` `y[]) ` `{ ` ` ` `int` `answer = 0; ` ` ` ` ` `// traversing through each points on the plane ` ` ` `int` `i = 0; ` ` ` `while` `(n--) { ` ` ` `// if sum of a points is greater than the ` ` ` `// previous one, the maximum gets replaced ` ` ` `if` `(x[i] + y[i] > answer) ` ` ` `answer = x[i] + y[i]; ` ` ` ` ` `i++; ` ` ` `} ` ` ` ` ` `// print the length ` ` ` `cout << ` `"Length -> "` `<< answer << endl; ` ` ` `cout << ` `"Path -> "` ` ` `<< ` `"( 1, "` `<< answer << ` `" )"` ` ` `<< ` `"and ( "` `<< answer << ` `", 1 )"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// initialize the number of points ` ` ` `int` `n = 4; ` ` ` ` ` `// points on the plane ` ` ` `int` `x[n] = { 1, 4, 2, 1 }; ` ` ` `int` `y[n] = { 4, 1, 1, 2 }; ` ` ` ` ` `shortestLength(n, x, y); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to illustrate ` `// the above problem ` `class` `GFG ` `{ ` ` ` `// function to get the minimum length of ` `// the shorter side of the triangle ` `static` `void` `shortestLength(` `int` `n, ` `int` `x[], ` ` ` `int` `y[]) ` `{ ` ` ` `int` `answer = ` `0` `; ` ` ` ` ` `// traversing through each ` ` ` `// points on the plane ` ` ` `int` `i = ` `0` `; ` ` ` `while` `(n != ` `0` `&& i < x.length) ` ` ` `{ ` ` ` `// if sum of a points is greater ` ` ` `// than the previous one, the ` ` ` `// maximum gets replaced ` ` ` `if` `(x[i] + y[i] > answer) ` ` ` `answer = x[i] + y[i]; ` ` ` ` ` `i++; ` ` ` `} ` ` ` ` ` `// print the length ` ` ` `System.out.println(` `"Length -> "` `+ answer ); ` ` ` `System.out.println(` `"Path -> "` `+ ` ` ` `"( 1, "` `+ answer + ` `" )"` `+ ` ` ` `"and ( "` `+ answer + ` `", 1 )"` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `// initialize the number of points ` ` ` `int` `n = ` `4` `; ` ` ` ` ` `// points on the plane ` ` ` `int` `x[] = ` `new` `int` `[] { ` `1` `, ` `4` `, ` `2` `, ` `1` `}; ` ` ` `int` `y[] = ` `new` `int` `[] { ` `4` `, ` `1` `, ` `1` `, ` `2` `}; ` ` ` ` ` `shortestLength(n, x, y); ` `} ` `} ` ` ` `// This code is contributed ` `// by Prerna Saini ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to illustrate ` `# the above problem ` ` ` `# function to get the minimum length of ` `# the shorter side of the triangle ` `def` `shortestLength(n, x, y): ` ` ` `answer ` `=` `0` ` ` ` ` `# traversing through each ` ` ` `# points on the plane ` ` ` `i ` `=` `0` ` ` `while` `n > ` `0` `: ` ` ` ` ` `# if sum of a points is greater ` ` ` `# than the previous one, the ` ` ` `# maximum gets replaced ` ` ` `if` `(x[i] ` `+` `y[i] > answer): ` ` ` `answer ` `=` `x[i] ` `+` `y[i] ` ` ` ` ` `i ` `+` `=` `1` ` ` `n ` `-` `=` `1` ` ` ` ` `# print the length ` ` ` `print` `(` `"Length -> "` `+` `str` `(answer)) ` ` ` `print` `( ` `"Path -> "` `+` ` ` `"( 1, "` `+` `str` `(answer)` `+` `" )"` `+` ` ` `"and ( "` `+` `str` `( answer) ` `+` `", 1 )"` `) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `# initialize the number of points ` ` ` `n ` `=` `4` ` ` ` ` `# points on the plane ` ` ` `x ` `=` `[ ` `1` `, ` `4` `, ` `2` `, ` `1` `] ` ` ` `y ` `=` `[ ` `4` `, ` `1` `, ` `1` `, ` `2` `] ` ` ` `shortestLength(n, x, y) ` ` ` `# This code is contributed ` `# by ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to illustrate ` `// the above problem ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// function to get the minimum ` `// length of the shorter side ` `// of the triangle ` `static` `void` `shortestLength(` `int` `n, ` `int` `[] x, ` ` ` `int` `[] y) ` `{ ` ` ` `int` `answer = 0; ` ` ` ` ` `// traversing through each ` ` ` `// points on the plane ` ` ` `int` `i = 0; ` ` ` `while` `(n != 0 && i < x.Length) ` ` ` `{ ` ` ` `// if sum of a points is greater ` ` ` `// than the previous one, the ` ` ` `// maximum gets replaced ` ` ` `if` `(x[i] + y[i] > answer) ` ` ` `answer = x[i] + y[i]; ` ` ` ` ` `i++; ` ` ` `} ` ` ` ` ` `// print the length ` ` ` `Console.WriteLine(` `"Length -> "` `+ answer); ` ` ` `Console.WriteLine(` `"Path -> "` `+ ` ` ` `"( 1, "` `+ answer + ` `" )"` `+ ` ` ` `"and ( "` `+ answer + ` `", 1 )"` `); ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` ` ` `// initialize the ` ` ` `// number of points ` ` ` `int` `n = 4; ` ` ` ` ` `// points on the plane ` ` ` `int` `[] x = ` `new` `int` `[] { 1, 4, 2, 1 }; ` ` ` `int` `[] y = ` `new` `int` `[] { 4, 1, 1, 2 }; ` ` ` ` ` `shortestLength(n, x, y); ` `} ` `} ` ` ` `// This code is contributed by Mahadev ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to illustrate ` `// the above problem ` ` ` `// function to get the minimum length of ` `// the shorter side of the triangle ` `function` `shortestLength(` `$n` `, &` `$x` `, &` `$y` `) ` `{ ` ` ` `$answer` `= 0; ` ` ` ` ` `// traversing through each ` ` ` `// points on the plane ` ` ` `$i` `= 0; ` ` ` `while` `(` `$n` `--) ` ` ` `{ ` ` ` ` ` `// if sum of a points is greater ` ` ` `// than the previous one, the ` ` ` `// maximum gets replaced ` ` ` `if` `(` `$x` `[` `$i` `] + ` `$y` `[` `$i` `] > ` `$answer` `) ` ` ` `$answer` `= ` `$x` `[` `$i` `] + ` `$y` `[` `$i` `]; ` ` ` ` ` `$i` `++; ` ` ` `} ` ` ` ` ` `// print the length ` ` ` `echo` `"Length -> "` `.` `$answer` `.` `"\n"` `; ` ` ` `echo` `"Path -> "` `. ` `"( 1, "` `.` `$answer` `.` `" )"` `. ` ` ` `"and ( "` `.` `$answer` `. ` `", 1 )"` `; ` `} ` ` ` `// Driver code ` ` ` `// initialize the number of points ` `$n` `= 4; ` ` ` `// points on the plane ` `$x` `= ` `array` `(1, 4, 2, 1 ); ` `$y` `= ` `array` `(4, 1, 1, 2 ); ` ` ` `shortestLength(` `$n` `, ` `$x` `, ` `$y` `); ` ` ` `// This code is contributed ` `// by ChitraNayal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Length -> 5 Path -> ( 1, 5 )and ( 5, 1 )

## Recommended Posts:

- Minimum Sum Path in a Triangle
- Minimum and maximum possible length of the third side of a triangle
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Shortest path in a Binary Maze
- Maximum path sum in a triangle.
- Maximum path sum in an Inverted triangle | SET 2
- Maximum sum of a path in a Right Number Triangle
- Minimum height of a triangle with given base and area
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Rectangle with minimum possible difference between the length and the width
- Minimum length of square to contain at least half of the given Coordinates
- Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle
- Biggest Reuleaux Triangle inscribed within a Square inscribed in an equilateral triangle
- Program to find Length of Bridge using Speed and Length of Train
- Length of the chord the circle if length of the another chord which is equally inclined through the diameter is given

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.