Count triplets from an array which can form quadratic equations with real roots | Java program for the above approach ; Function to count the number of triplets ( a , b , c ) such that the equation ax ^ 2 + bx + c = 0 has real roots ; Sort the array in ascending order ; Stores count of triplets ( a , b , c ) such that ax ^ 2 + bx + c = 0 has real roots ; Base case ; Traverse the given array ; If values 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 ; Function Call
import java . util . * ; class GFG { static int getCount ( int arr [ ] , int N ) { Arrays . sort ( arr ) ; int count = 0 ; if ( N < 3 ) return 0 ; for ( int b = 0 ; b < N ; b ++ ) { int a = 0 , c = N - 1 ; int d = arr [ b ] * arr [ b ] / 4 ; while ( a < c ) { if ( a == b ) { a ++ ; continue ; } if ( c == b ) { c -- ; continue ; } if ( arr [ a ] * arr <= d ) { if ( a < b && b < c ) { count += c - a - 1 ; } else { count += c - a ; } a ++ ; } else { c -- ; } } } return count * 2 ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 6 , 10 , 13 , 21 } ; int N = arr . length ; System . out . print ( getCount ( arr , N ) ) ; } }