Count of acute, obtuse and right triangles with given sides
Last Updated :
12 Sep, 2023
Given an array of n positive distinct integers representing lengths of lines that can form a triangle. The task is to find the number of acute triangles, obtuse triangles, and right triangles separately that can be formed from the given array.
Examples:
Input : arr[] = { 2, 3, 9, 10, 12, 15 }.
Output :
Acute Triangle: 2
Right Triangle: 1
Obtuse Triangle: 4
Acute triangles that can be formed are {10, 12, 15}
and { 9, 10, 12 }.
Right triangles that can be formed are {9, 12, 15}.
Obtuse triangles that can be formed are {2, 9, 10},
{3, 9, 10}, {3, 10, 12} and {9, 10, 15}.
Triangle having edges a, b, c, a <= b c.
If a2 + b2 > c2, then it is acute triangle.
If a2 + b2 = c2, then it is right triangle.
If a2 + b2 < c2, then it is obtuse triangle.
Method 1 (Simple): A brute force can be, use three loops, one for each side. Check the above three conditions if a triangle is possible from three sides.
Method 2 (Efficient): An efficient approach is to first sort the array and run two loops for side a and b (a<b). Then find the farthest point q where a + b > c. So, from b to q all triangle are possible.
Also find a farthest point p where a2 + b2 >= c2.
Now, observe if a2 + b2 = c2, then increment count of right triangles. Also, the acute triangle will be p – b – 1, and the obtuse triangle will be q – p.
C++
#include <bits/stdc++.h>
using namespace std;
void findTriangle( int a[], int n)
{
int b[n + 2];
for ( int i = 0; i < n; i++)
b[i] = a[i] * a[i];
sort(a, a + n);
sort(b, b + n);
int x=0,y=0,z=0;
for ( int i=0; i<n; i++)
{
int p = i+1;
int q = i+1;
for ( int j=i+1; j<n; j++)
{
while (p<n-1 && b[i]+b[j]>=b[p+1])
p++;
q = max(q, p);
while (q<n-1 && a[i]+a[j]>a[q+1])
q++;
if (b[i]+b[j]==b[p])
{
x += max(p - j - 1, 0);
y++;
z += q - p;
}
else
{
x += max(p - j, 0);
z += q - p;
}
}
}
cout << "Acute Triangle: " << x << endl;
cout << "Right Triangle: " << y << endl;
cout << "Obtuse Triangle: " << z << endl;
}
int main()
{
int arr[] = {2, 3, 9, 10, 12, 15};
int n = sizeof (arr)/ sizeof (arr[0]);
findTriangle(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findTriangle( int a[],
int n)
{
int b[] = new int [n];
for ( int i = 0 ; i < n; i++)
b[i] = a[i] * a[i];
Arrays.sort(a);
Arrays.sort(b);
int x = 0 , y = 0 , z = 0 ;
for ( int i = 0 ; i < n; i++)
{
int p = i + 1 ;
int q = i + 1 ;
for ( int j = i + 1 ; j < n; j++)
{
while (p < n - 1 &&
b[i] + b[j] >= b[p + 1 ])
p++;
q = Math.max(q, p);
while (q < n - 1 &&
a[i] + a[j] > a[q + 1 ])
q++;
if (b[i] + b[j] == b[p])
{
x += Math.max(p - j - 1 , 0 );
y++;
z += q - p;
}
else
{
x += Math.max(p - j, 0 );
z += q - p;
}
}
}
System.out.println( "Acute Triangle: " + x);
System.out.println( "Right Triangle: " + y);
System.out.println( "Obtuse Triangle: " + z);
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 9 , 10 , 12 , 15 };
int n = arr.length;
findTriangle(arr, n);
}
}
|
Python3
def findTriangle(a, n) :
b = []
for i in range (n) :
b.append(a[i] * a[i])
a.sort()
b.sort()
x, y, z = 0 , 0 , 0
for i in range (n) :
p = i + 1
q = i + 1
for j in range (i + 1 , n) :
while (p<n - 1 and b[i] + b[j]> = b[p + 1 ]) :
p + = 1
q = max (q, p)
while (q<n - 1 and a[i] + a[j]>a[q + 1 ]) :
q + = 1
if (b[i] + b[j] = = b[p]) :
x + = max (p - j - 1 , 0 )
y + = 1
z + = q - p
else :
x + = max (p - j, 0 )
z + = q - p
print ( "Acute Triangle:" ,x )
print ( "Right Triangle:" , y)
print ( "Obtuse Triangle:" , z)
if __name__ = = "__main__" :
arr = [ 2 , 3 , 9 , 10 , 12 , 15 ]
n = len (arr)
findTriangle(arr, n)
|
C#
using System;
class GFG{
static void findTriangle( int []a, int n)
{
int []b = new int [n];
for ( int i = 0; i < n; i++)
b[i] = a[i] * a[i];
Array.Sort(a);
Array.Sort(b);
int x = 0, y = 0, z = 0;
for ( int i = 0; i < n; i++)
{
int p = i + 1;
int q = i + 1;
for ( int j = i + 1; j < n; j++)
{
while (p < n - 1 &&
b[i] + b[j] >= b[p + 1])
p++;
q = Math.Max(q, p);
while (q < n - 1 &&
a[i] + a[j] > a[q + 1])
q++;
if (b[i] + b[j] == b[p])
{
x += Math.Max(p - j - 1, 0);
y++;
z += q - p;
}
else
{
x += Math.Max(p - j, 0);
z += q - p;
}
}
}
Console.Write( "Acute Triangle: " + x + "\n" );
Console.Write( "Right Triangle: " + y + "\n" );
Console.Write( "Obtuse Triangle: " + z + "\n" );
}
public static void Main( string [] args)
{
int []arr = { 2, 3, 9, 10, 12, 15 };
int n = arr.Length;
findTriangle(arr, n);
}
}
|
PHP
<?php
function findTriangle( $a , $n )
{
$b [ $n + 2] = array ();
for ( $i = 0; $i < $n ; $i ++)
$b [ $i ] = $a [ $i ] * $a [ $i ];
sort( $a );
sort( $b );
$x = 0;
$y = 0;
$z = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$p = $i + 1;
$q = $i + 1;
for ( $j = $i + 1; $j < $n ; $j ++)
{
while ( $p < $n - 1 &&
$b [ $i ] + $b [ $j ] >= $b [ $p + 1])
$p ++;
$q = max( $q , $p );
while ( $q < $n - 1 &&
$a [ $i ] + $a [ $j ] > $a [ $q + 1])
$q ++;
if ( $b [ $i ] + $b [ $j ] == $b [ $p ])
{
$x += max( $p - $j - 1, 0);
$y ++;
$z += $q - $p ;
}
else
{
$x += max( $p - $j , 0);
$z += $q - $p ;
}
}
}
echo "Acute Triangle: " , $x , "\n" ;
echo "Right Triangle: " , $y , "\n" ;
echo "Obtuse Triangle: " , $z , "\n" ;
}
$arr = array (2, 3, 9, 10, 12, 15);
$n = sizeof( $arr );
findTriangle( $arr , $n );
?>
|
Javascript
<script>
function findTriangle(a,n)
{
let b= new Array(n);
for (let i=0;i<n;i++)
{
b[i]=0;
}
for (let i = 0; i < n; i++)
b[i] = a[i] * a[i];
a.sort( function (i,j){ return i-j;});
b.sort( function (i,j){ return i-j;});
let x = 0, y = 0, z = 0;
for (let i = 0; i < n; i++)
{
let p = i + 1;
let q = i + 1;
for (let j = i + 1; j < n; j++)
{
while (p < n - 1 &&
b[i] + b[j] >= b[p + 1])
p++;
q = Math.max(q, p);
while (q < n - 1 &&
a[i] + a[j] > a[q + 1])
q++;
if (b[i] + b[j] == b[p])
{
x += Math.max(p - j - 1, 0);
y++;
z += q - p;
}
else
{
x += Math.max(p - j, 0);
z += q - p;
}
}
}
document.write( "Acute Triangle: " + x+ "<br>" );
document.write( "Right Triangle: " + y+ "<br>" );
document.write( "Obtuse Triangle: " + z+ "<br>" );
}
let arr=[2, 3, 9, 10, 12, 15];
let n = arr.length;
findTriangle(arr, n);
</script>
|
Output:
Acute Triangle: 2
Right Triangle: 1
Obtuse Triangle: 4
Time Complexity: O(n^3), as in the worst case we are iterating through three loops up to n.
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...