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++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
void shortestLength( int n, int x[], int y[])
{
int answer = 0;
int i = 0;
while (n--) {
if (x[i] + y[i] > answer)
answer = x[i] + y[i];
i++;
}
cout << "Length -> " << answer << endl;
cout << "Path -> "
<< "( 1, " << answer << " )"
<< "and ( " << answer << ", 1 )" ;
}
int main()
{
int n = 4;
int x[n] = { 1, 4, 2, 1 };
int y[n] = { 4, 1, 1, 2 };
shortestLength(n, x, y);
return 0;
}
|
Java
class GFG
{
static void shortestLength( int n, int x[],
int y[])
{
int answer = 0 ;
int i = 0 ;
while (n != 0 && i < x.length)
{
if (x[i] + y[i] > answer)
answer = x[i] + y[i];
i++;
}
System.out.println( "Length -> " + answer );
System.out.println( "Path -> " +
"( 1, " + answer + " )" +
"and ( " + answer + ", 1 )" );
}
public static void main(String[] args)
{
int n = 4 ;
int x[] = new int [] { 1 , 4 , 2 , 1 };
int y[] = new int [] { 4 , 1 , 1 , 2 };
shortestLength(n, x, y);
}
}
|
Python 3
def shortestLength(n, x, y):
answer = 0
i = 0
while n > 0 :
if (x[i] + y[i] > answer):
answer = x[i] + y[i]
i + = 1
n - = 1
print ( "Length -> " + str (answer))
print ( "Path -> " +
"( 1, " + str (answer) + " )" +
"and ( " + str ( answer) + ", 1 )" )
if __name__ = = "__main__" :
n = 4
x = [ 1 , 4 , 2 , 1 ]
y = [ 4 , 1 , 1 , 2 ]
shortestLength(n, x, y)
|
C#
using System;
class GFG
{
static void shortestLength( int n, int [] x,
int [] y)
{
int answer = 0;
int i = 0;
while (n != 0 && i < x.Length)
{
if (x[i] + y[i] > answer)
answer = x[i] + y[i];
i++;
}
Console.WriteLine( "Length -> " + answer);
Console.WriteLine( "Path -> " +
"( 1, " + answer + " )" +
"and ( " + answer + ", 1 )" );
}
static public void Main ()
{
int n = 4;
int [] x = new int [] { 1, 4, 2, 1 };
int [] y = new int [] { 4, 1, 1, 2 };
shortestLength(n, x, y);
}
}
|
PHP
<?php
function shortestLength( $n , & $x , & $y )
{
$answer = 0;
$i = 0;
while ( $n --)
{
if ( $x [ $i ] + $y [ $i ] > $answer )
$answer = $x [ $i ] + $y [ $i ];
$i ++;
}
echo "Length -> " . $answer . "\n" ;
echo "Path -> " . "( 1, " . $answer . " )" .
"and ( " . $answer . ", 1 )" ;
}
$n = 4;
$x = array (1, 4, 2, 1 );
$y = array (4, 1, 1, 2 );
shortestLength( $n , $x , $y );
?>
|
Javascript
<script>
function shortestLength(n, x, y)
{
let answer = 0;
let i = 0;
while (n != 0 && i < x.length)
{
if (x[i] + y[i] > answer)
answer = x[i] + y[i];
i++;
}
document.write( "Length -> " + answer + "<br/>" );
document.write( "Path -> " +
"( 1, " + answer + " )" +
"and ( " + answer + ", 1 )" );
}
let n = 4;
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)