Count triplets from an array which can form quadratic equations with real roots | Function to count the number of triplets ( a , b , c ) Such that the equations ax ^ 2 + bx + c = 0 has real roots ; sort he array in ascending order ; store count of triplets ( a , b , c ) such that ax ^ 2 + bx + c = 0 has real roots ; base case ; Traverse the given array ; if value of a and c are equal to b ; increment a ; Decrement c ; condition for having real roots for a quadratic equation ; if b lies in between a and c ; update count ; update count ; increment a ; Decrement c ; for each pair two values are possible of a and c ; Driver code ;
def getcount ( arr , N ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE count = 0 NEW_LINE if ( N < 3 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT for b in range ( 0 , N ) : NEW_LINE INDENT a = 0 NEW_LINE c = N - 1 NEW_LINE d = arr [ b ] * arr [ b ] // 4 NEW_LINE while ( a < c ) : NEW_LINE INDENT if ( a == b ) : NEW_LINE INDENT a += 1 NEW_LINE continue NEW_LINE DEDENT if ( c == b ) : NEW_LINE INDENT c -= 1 NEW_LINE continue NEW_LINE DEDENT if ( arr [ a ] * arr ) <= d : NEW_LINE INDENT if ( a < b and b < c ) : NEW_LINE INDENT count += c - a - 1 NEW_LINE DEDENT else : NEW_LINE INDENT count += c - a NEW_LINE DEDENT a += 1 NEW_LINE DEDENT else : NEW_LINE INDENT c -= 1 NEW_LINE DEDENT DEDENT DEDENT return count * 2 NEW_LINE DEDENT arr = [ 3 , 6 , 10 , 13 , 21 ] NEW_LINE N = len ( arr ) NEW_LINE / * Function Call * / NEW_LINE print ( getcount ( arr , N ) ) NEW_LINE