Count of acute , obtuse and right triangles with given sides | Find the number of acute , right , obtuse triangle that can be formed from given array . ; Finding the square of each element of array ; Sort the sides of array and their squares . ; x for acute triangles y for right triangles z for obtuse triangles ; Finding the farthest point p where a ^ 2 + b ^ 2 >= c ^ 2. ; Finding the farthest point q where a + b > c . ; If point p make right triangle . ; All triangle between j and p are acute triangles . So add p - j - 1 in x . ; Increment y by 1. ; All triangle between q and p are acute triangles . So add q - p in z . ; If no right triangle ; All triangle between j and p are acute triangles . So add p - j in x . ; All triangle between q and p are acute triangles . So add q - p in z . ; Driver Code
def findTriangle ( a , n ) : NEW_LINE INDENT b = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT b . append ( a [ i ] * a [ i ] ) NEW_LINE DEDENT a . sort ( ) NEW_LINE b . sort ( ) NEW_LINE x , y , z = 0 , 0 , 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT p = i + 1 NEW_LINE q = i + 1 NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT while ( p < n - 1 and b [ i ] + b [ j ] >= b [ p + 1 ] ) : NEW_LINE INDENT p += 1 NEW_LINE DEDENT q = max ( q , p ) NEW_LINE while ( q < n - 1 and a [ i ] + a [ j ] > a [ q + 1 ] ) : NEW_LINE INDENT q += 1 NEW_LINE DEDENT if ( b [ i ] + b [ j ] == b [ p ] ) : NEW_LINE INDENT x += max ( p - j - 1 , 0 ) NEW_LINE y += 1 NEW_LINE z += q - p NEW_LINE DEDENT else : NEW_LINE INDENT x += max ( p - j , 0 ) NEW_LINE z += q - p NEW_LINE DEDENT DEDENT DEDENT print ( " Acute ▁ Triangle : " , x ) NEW_LINE print ( " Right ▁ Triangle : " , y ) NEW_LINE print ( " Obtuse ▁ Triangle : " , z ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 9 , 10 , 12 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE findTriangle ( arr , n ) NEW_LINE DEDENT