Minimum length of the shortest path of a triangle
Last Updated :
08 Jun, 2022
Given N points on the plane, ( X1, Y1 ), ( X2, Y2 ), ( X3, Y3 ), ......, ( XN, YN ). 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;
}
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
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
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
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
?>
JavaScript
<script>
// Javascript program to illustrate
// the above problem
// function to get the minimum length of
// the shorter side of the triangle
function shortestLength(n, x, y)
{
let answer = 0;
// traversing through each
// points on the plane
let 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
document.write("Length -> " + answer + "<br/>");
document.write("Path -> " +
"( 1, " + answer + " )" +
"and ( " + answer + ", 1 )");
}
// Driver Code
// initialize the number of points
let n = 4;
// points on the plane
let x = [ 1, 4, 2, 1 ];
let y = [ 4, 1, 1, 2 ];
shortestLength(n, x, y);
</script>
Output: Length -> 5
Path -> ( 1, 5 )and ( 5, 1 )
Time Complexity: O(n) where n is the number of points.
Auxiliary Space: O(1)
Similar Reads
Minimum and maximum possible length of the third side of a triangle
Given two sides of a triangle s1 and s2, the task is to find the minimum and maximum possible length of the third side of the given triangle. Print -1 if it is not possible to make a triangle with the given side lengths. Note that the length of all the sides must be integers.Examples: Input: s1 = 3,
5 min read
Sum of all pair shortest paths in a Tree
Given a weighted undirected graph T consisting of nodes valued [0, N - 1] and an array Edges[][3] of type {u, v, w} that denotes an edge between vertices u and v having weight w. The task is to find the sum of all pair shortest paths in the given tree. Examples: Input: N = 3, Edges[][] = {{0, 2, 15}
15+ min read
Maximum sum of a path in a Right Number Triangle
Given a right triangle of numbers, find the largest of the sum of numbers that appear on the paths starting from the top towards the base, so that on each path the next number is located directly below or below and one place to the right. Examples :Â Input : 1 1 2 4 1 2 2 3 1 1 Output : 9 Explanatio
15+ min read
Dijkstra's shortest path with minimum edges
Prerequisite: Dijkstraâs shortest path algorithm Given an adjacency matrix graph representing paths between the nodes in the given graph. The task is to find the shortest path with minimum edges i.e. if there a multiple short paths with same cost then choose the one with the minimum number of edges.
13 min read
Number of shortest paths in an Undirected Weighted Graph
Given a weighted undirected graph G and an integer S, the task is to print the distances of the shortest paths and the count of the number of the shortest paths for each node from a given vertex, S. Examples: Input: S =1, G = Output: Shortest Paths distances are : 0 1 2 4 5 3 2 1 3 Numbers of the sh
15 min read
Shortest paths from all vertices to a destination
Given a Weighted Directed Graph and a destination vertex in the graph, find the shortest distance from all vertex to the destination vertex.Input : 1 Output : 0 6 10 7 5Distance of 0 from 0: 0 Distance of 0 from 1: 1+5 = 6 (1->4->0) Distance of 0 from 2: 10 (2->0) Distance of 0 from 3: 1+1+
11 min read
Maximum path sum in a triangle.
We have given numbers in form of a triangle, by starting at the top of the triangle and moving to adjacent numbers on the row below, find the maximum total from top to bottom. Examples : Input : 3 7 4 2 4 6 8 5 9 3 Output : 23 Explanation : 3 + 7 + 4 + 9 = 23 Input : 8 -4 4 2 2 6 1 1 1 1 Output : 19
15+ min read
Minimize cost to reach end of an N-length straight path
Given an integer K denoting the fuel tank capacity of a car running at the cost of 1 liter / mtr on a straight path of length N meters and two arrays a[] and b[], each of size M, where a[i] denotes the location of the ith station and b[i] denotes the cost of 1-liter of fuel at that station. The task
12 min read
Maximum path sum in an Inverted triangle | SET 2
Given numbers in form of an Inverted triangle. By starting at the bottom of the triangle and moving to adjacent numbers on the row above, find the maximum total from bottom to top.Examples: Input : 1 5 3 4 8 1 Output : 14 Input : 8 5 9 3 2 4 6 7 4 3 Output : 23 Method 1: Recursion In the previous ar
13 min read
Find the length of the median of a Triangle if length of sides are given
Given the length of all three sides of a triangle as a, b and c. The task is to calculate the length of the median of the triangle. A median of a triangle is a line segment joining a vertex to the midpoint of the opposite side, thus bisecting that side.  Examples:  Input: a = 8, b = 10, c = 13 Ou
4 min read