Given an array arr[] consisting of N distinct positive integers, the task is to find the count of triplets (a, b, c) such that the quadratic equation aX2 + bX + c = 0 has real roots.
Examples:
Input: arr[] = { 2, 3, 6, 8 }
Output: 6
Explanation:
For the triplets (a = 2, b = 6, c = 3), (a = 3, b = 6, c = 2), (a = 2, b = 8, c = 3), (a = 3, b = 8, c = 2), (a = 2, b = 8, c = 6), (a = 6, b = 8, c = 2)}, the quadratic equation 2X2 + 6X + 3 = 0 has real roots.
Input: arr[] = [1, 2, 3, 4, 5]
Output: 14
Naive Approach: A quadratic equation has real roots if its determinant is less than or equal to 0. Therefore, a * c ? b * b / 4. The problem can be solved by using this property.
Follow the steps given below to solve the problem:
- Iterate over the range [0, N – 1] using a variable, say b, and perform the following steps:
- For each value of b, run a loop from a = 0 to N – 1 and check if b and a are equal or not. If found to be true, then continue the loop.
- Now, iterate over the range [0, N – 1] using a variable, say c, and check if both b = c or a = c are satisfied or not. If found to be true, then continue the loop.
- If arr[a] * arr[C] ? arr[b] * arr[b] / 4, then increment the count.
- Finally, return the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getCount( int arr[], int N)
{
int count = 0;
if (N < 3)
return 0;
for ( int b = 0; b < N; b++) {
for ( int a = 0; a < N; a++) {
if (a == b)
continue ;
for ( int c = 0; c < N; c++) {
if (c == a || c == b)
continue ;
int d = arr[b] * arr[b] / 4;
if (arr[a] * arr <= d)
count++;
}
}
}
return count;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << getCount(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int getCount( int arr[], int N)
{
int count = 0 ;
if (N < 3 )
return 0 ;
for ( int b = 0 ; b < N; b++)
{
for ( int a = 0 ; a < N; a++)
{
if (a == b)
continue ;
for ( int c = 0 ; c < N; c++)
{
if (c == a || c == b)
continue ;
int d = arr[b] * arr[b] / 4 ;
if (arr[a] * arr <= d)
count++;
}
}
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int N = arr.length;
System.out.print(getCount(arr, N));
}
}
|
Python3
def getcount(arr,N):
count = 0
if (N < 3 ):
return 0
for b in range ( 0 , N):
for a in range ( 0 , N):
if (a = = b):
continue
for c in range ( 0 , N):
if (c = = a or c = = b):
continue
d = arr[b] * arr[b] / / 4
if (arr[a] * arr) < = d:
count + = 1
return count
arr = [ 1 , 2 , 3 , 4 , 5 ]
N = len (arr)
print (getcount(arr, N))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static int getCount( int [] arr, int N)
{
int count = 0;
if (N < 3)
return 0;
for ( int b = 0; b < N; b++)
{
for ( int a = 0; a < N; a++)
{
if (a == b)
continue ;
for ( int c = 0; c < N; c++)
{
if (c == a || c == b)
continue ;
int d = arr[b] * arr[b] / 4;
if (arr[a] * arr <= d)
count++;
}
}
}
return count;
}
static public void Main()
{
int [] arr = { 1, 2, 3, 4, 5 };
int N = arr.Length;
Console.WriteLine(getCount(arr, N));
}
}
|
Javascript
<script>
function getCount(arr, N)
{
var count = 0;
if (N < 3)
return 0;
for ( var b = 0; b < N; b++)
{
for ( var a = 0; a < N; a++)
{
if (a == b)
continue ;
for ( var c = 0; c < N; c++)
{
if (c == a || c == b)
continue ;
var d = arr[b] * arr[b] / 4;
if (arr[a] * arr <= d)
count++;
}
}
}
return count;
}
var arr = [ 1, 2, 3, 4, 5 ];
var N = arr.length;
document.write(getCount(arr, N));
</script>
|
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient approach: The problem can be solved efficiently by Using sorting and two pointers algorithm.
Follow the below steps to solve the problem:
- Sort the given array arr[] in ascending order.
- Run a loop from b = 0 to the size of the array.
- Initialize two variables a and c with 0 and size of array equal to -1, respectively.
- Run a loop while a < c holds true and check for the following conditions:
- If a = b, then increment a and continue the loop.
- If c = b, then decrement c and continue the loop.
- If arr[a] * arr[C] ? d, then check for the following conditions:
- If a < b < c , then increase count by number of elements between them – 1(except arr[b]).
- Otherwise, increase count by the number of elements between them.
- Otherwise, decrement c.
- For each pair, two values are possible of a and c. So return count * 2.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getCount( int arr[], int N)
{
sort(arr, arr + N);
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;
}
int main()
{
int arr[] = { 3, 6, 10, 13, 21 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << getCount(arr, N);
return 0;
}
|
Java
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));
}
}
|
Python3
def getcount(arr, N):
arr.sort()
count = 0
if (N < 3 ):
return 0
for b in range ( 0 , N):
a = 0
c = N - 1
d = arr[b] * arr[b] / / 4
while (a < c):
if (a = = b):
a + = 1
continue
if (c = = b):
c - = 1
continue
if (arr[a] * arr) < = d:
if (a < b and b < c):
count + = c - a - 1
else :
count + = c - a
a + = 1
else :
c - = 1
return count * 2
arr = [ 3 , 6 , 10 , 13 , 21 ]
N = len (arr)
print (getcount(arr,N))
|
C#
using System;
class GFG{
static int getCount( int [] arr, int N)
{
Array.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;
}
static public void Main()
{
int [] arr = { 3, 6, 10, 13, 21 };
int N = arr.Length;
Console.Write(getCount(arr, N));
}
}
|
Javascript
<script>
function getCount(arr, N)
{
arr.sort();
var count = 0;
if (N < 3)
return 0;
for ( var b = 0; b < N; b++)
{
var a = 0, c = N - 1;
var 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;
}
var arr = [ 3, 6, 10, 13, 21 ];
var N = arr.length;
document.write(getCount(arr, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)