📜  从数组中计算三元组,该数组可以形成具有实根的二次方程式

📅  最后修改于: 2021-04-17 18:51:53             🧑  作者: Mango

给定一个由N个不同的正整数组成的数组arr [] ,任务是查找三元组(a,b,c)的数量,以使二次方程aX 2 + bX + c = 0具有实根

例子:

天真的方法:如果二次方程的行列式小于或等于0,则该二次方程具有实根。因此, a * c≤b * b / 4 。使用此属性可以解决该问题。
请按照以下步骤解决问题:

  • 使用变量b遍历[0,N – 1]范围,并执行以下步骤:
    1. 对于每个b值,运行一个从a = 0N – 1的循环,并检查ba是否相等。如果发现为真,则继续循环。
    2. 现在,使用变量c遍历[0,N – 1]范围,并检查b = ca = c是否都满足。如果发现为真,则继续循环。
    3. 如果arr [a] * arr [C]≤arr [b] * arr [b] / 4 ,则增加计数。
  • 最后,返回count

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find count of triplets (a, b, c)
// such that the equations ax^2 + bx + c = 0
// has real roots
int getCount(int arr[], int N)
{
 
    // Stores count of triplets(a, b, c) such
    // that ax^2 + bx + c = 0 has real roots
    int count = 0;
     
    // Base case
    if (N < 3)
        return 0;
 
    // Generate all possible triplets(a, b, c)
    for (int b = 0; b < N; b++) {
        for (int a = 0; a < N; a++) {
         
            // If the coefficient of
            // X^2 and X are equal
            if (a == b)
                continue;
 
            for (int c = 0; c < N; c++) {
             
                // If coefficient of X^2
                // or x are equal to the
                // constant
                if (c == a || c == b)
                    continue;
 
                int d = arr[b] * arr[b] / 4;
             
                // Condition for having
                // real roots
                if (arr[a] * arr <= d)
                    count++;
            }
        }
    }
    return count;
}
 
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << getCount(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find count of triplets (a, b, c)
// such that the equations ax^2 + bx + c = 0
// has real roots
static int getCount(int arr[], int N)
{
 
    // Stores count of triplets(a, b, c) such
    // that ax^2 + bx + c = 0 has real roots
    int count = 0;
     
    // Base case
    if (N < 3)
        return 0;
 
    // Generate all possible triplets(a, b, c)
    for (int b = 0; b < N; b++)
    {
        for (int a = 0; a < N; a++)
        {
         
            // If the coefficient of
            // X^2 and X are equal
            if (a == b)
                continue;
 
            for (int c = 0; c < N; c++)
            {
             
                // If coefficient of X^2
                // or x are equal to the
                // constant
                if (c == a || c == b)
                    continue;
                int d = arr[b] * arr[b] / 4;
             
                // Condition for having
                // real roots
                if (arr[a] * arr <= d)
                    count++;
            }
        }
    }
    return count;
}
 
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int N = arr.length;
 
    // Function Call
    System.out.print(getCount(arr, N));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python Program for the above approach
 
# Function to find the count of triplets(a,b,c)
# Such that the equations ax^2 + bx + c = 0
# has real roots
 
def getcount(arr,N):
     
    # store count of triplets(a,b,c) such
    # that ax^2 + bx + c = 0 has real roots
    count = 0
 
    # base case
    if (N < 3):
        return 0
     
    # Generate all possible triplets (a,b,c)
    for b in range(0, N):
        for a in range(0, N):
           
            # if the coefficient of X^2
            # and x are equal
            if (a == b):
                continue
            for c in range(0, N):
               
                # if coefficient of X^2
                # or x are equal to the
                # constant
                if (c == a or c == b):
                    continue
                d = arr[b] * arr[b] // 4
                 
                # condition for having
                # real roots
                if (arr[a] * arr) <= d:
                    count += 1
    return count
 
# Driver code
arr = [1,2,3,4,5]
N = len(arr)
print(getcount(arr, N))
 
# This code is contributed by Virusbuddah


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG
{
 
  // Function to find count of triplets (a, b, c)
  // such that the equations ax^2 + bx + c = 0
  // has real roots
  static int getCount(int[] arr, int N)
  {
 
    // Stores count of triplets(a, b, c) such
    // that ax^2 + bx + c = 0 has real roots
    int count = 0;
 
    // Base case
    if (N < 3)
      return 0;
 
    // Generate all possible triplets(a, b, c)
    for (int b = 0; b < N; b++)
    {
      for (int a = 0; a < N; a++)
      {
 
        // If the coefficient of
        // X^2 and X are equal
        if (a == b)
          continue;
 
        for (int c = 0; c < N; c++)
        {
 
          // If coefficient of X^2
          // or x are equal to the
          // constant
          if (c == a || c == b)
            continue;
          int d = arr[b] * arr[b] / 4;
 
          // Condition for having
          // real roots
          if (arr[a] * arr <= d)
            count++;
        }
      }
    }
    return count;
  }
 
  // Driver Code
  static public void Main()
  {
    int[] arr = { 1, 2, 3, 4, 5 };
    int N = arr.Length;
 
    // Function Call
    Console.WriteLine(getCount(arr, N));
  }
}
 
// This code is contributed by sanjoy_62.


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the number of triplets
// (a, b, c) such that the equation
// ax^2 + bx + c = 0 has real roots
int getCount(int arr[], int N)
{
    // Sort the array in
    // ascending order
    sort(arr, arr + N);
 
    // Stores count of triplets(a, b, c) such
    // that ax^2 + bx + c = 0 has real roots
    int count = 0;
     
    // Base case
    if (N < 3)
        return 0;
 
    // Traverse the given array
    for (int b = 0; b < N; b++) {
 
        int a = 0, c = N - 1;
        int d = arr[b] * arr[b] / 4;
 
        while (a < c) {
 
            // If values of a and
            // c are equal to b
            if (a == b) {
                 
                // Increment a
                a++;
                continue;
            }
            if (c == b) {
                 
                // Decrement c
                c--;
                continue;
            }
 
            // Condition for having real
            // roots for a quadratic equation
            if (arr[a] * arr <= d) {
 
                // If b lies in
                // between a and c
                if (a < b && b < c) {
                     
                     
                    // Update count
                    count += c - a - 1;
                }
                else {
                     
                    // Update count
                    count += c - a;
                }
                 
                // Increment a
                a++;
            }
            else {
                 
                // Decrement c
                c--;
            }
        }
    }
 
    // For each pair two values
    // are possible of a and c
    return count * 2;
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 6, 10, 13, 21 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << getCount(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to count the number of triplets
// (a, b, c) such that the equation
// ax^2 + bx + c = 0 has real roots
static int getCount(int arr[], int N)
{
   
    // Sort the array in
    // ascending order
    Arrays.sort(arr);
 
    // Stores count of triplets(a, b, c) such
    // that ax^2 + bx + c = 0 has real roots
    int count = 0;
     
    // Base case
    if (N < 3)
        return 0;
 
    // Traverse the given array
    for (int b = 0; b < N; b++)
    {
        int a = 0, c = N - 1;
        int d = arr[b] * arr[b] / 4;
        while (a < c)
        {
 
            // If values of a and
            // c are equal to b
            if (a == b)
            {
                 
                // Increment a
                a++;
                continue;
            }
            if (c == b)
            {
                 
                // Decrement c
                c--;
                continue;
            }
 
            // Condition for having real
            // roots for a quadratic equation
            if (arr[a] * arr <= d)
            {
 
                // If b lies in
                // between a and c
                if (a < b && b < c)
                {
                       
                    // Update count
                    count += c - a - 1;
                }
                else
                {
                     
                    // Update count
                    count += c - a;
                }
                 
                // Increment a
                a++;
            }
            else {
                 
                // Decrement c
                c--;
            }
        }
    }
 
    // For each pair two values
    // are possible of a and c
    return count * 2;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 3, 6, 10, 13, 21 };
    int N = arr.length;
 
    // Function Call
    System.out.print(getCount(arr, N));
}
}
 
// This code is contributed by code_hunt.


Python3
# Python3 Program for the above approach
 
# Function to  count the number of triplets(a,b,c)
# Such that the equations ax^2 + bx + c = 0
# has real roots
def getcount(arr, N):
 
    # sort he array in
    # ascending order
    arr.sort()
 
    # store count of triplets (a,b,c) such
    # that ax^2 + bx + c = 0 has real roots
    count = 0
 
    # base case
    if (N < 3):
        return 0
       
    # Traverse the given array
    for b in range(0, N):
        a = 0
        c = N - 1
        d = arr[b] * arr[b] // 4
        while(a < c):
           
            # if value of a and
            # c are equal to b
            if (a == b):
               
                # increment a
                a += 1
                continue
            if (c == b):
               
                # Decrement c
                c -= 1
                continue
                 
            # condition for having real
            # roots for a quadratic equation
            if (arr[a] * arr) <= d:
               
                # if b lies in
                # between a and c
                if (a < b and b < c):
                   
                    # update count
                    count += c - a - 1
                else:
                   
                    # update count
                    count += c - a
                     
                # increment a
                a += 1
            else:
               
                # Decrement c
                c -= 1
                 
    # for each pair two values
    # are possible of a and c           
    return count * 2
 
# Driver code
arr = [3,6,10,13,21]
N = len(arr)
print(getcount(arr,N))
 
# This code is contributed by Virusbuddah


C#
// C# program for the above approach
using System;
 
class GFG{
 
  // Function to count the number of triplets
  // (a, b, c) such that the equation
  // ax^2 + bx + c = 0 has real roots
  static int getCount(int[] arr, int N)
  {
 
    // Sort the array in
    // ascending order
    Array.Sort(arr);
 
    // Stores count of triplets(a, b, c) such
    // that ax^2 + bx + c = 0 has real roots
    int count = 0;
 
    // Base case
    if (N < 3)
      return 0;
 
    // Traverse the given array
    for (int b = 0; b < N; b++)
    {
      int a = 0, c = N - 1;
      int d = arr[b] * arr[b] / 4;
      while (a < c)
      {
 
        // If values of a and
        // c are equal to b
        if (a == b)
        {
 
          // Increment a
          a++;
          continue;
        }
        if (c == b)
        {
 
          // Decrement c
          c--;
          continue;
        }
 
        // Condition for having real
        // roots for a quadratic equation
        if (arr[a] * arr <= d)
        {
 
          // If b lies in
          // between a and c
          if (a < b && b < c)
          {
 
            // Update count
            count += c - a - 1;
          }
          else
          {
 
            // Update count
            count += c - a;
          }
 
          // Increment a
          a++;
        }
        else {
 
          // Decrement c
          c--;
        }
      }
    }
 
    // For each pair two values
    // are possible of a and c
    return count * 2;
  }
 
 
  // Driver Code
  static public void Main()
  {
    int[] arr = { 3, 6, 10, 13, 21 };
    int N = arr.Length;
 
    // Function Call
    Console.Write(getCount(arr, N));
  }
}
 
// This code is contributed by susmitakundugoaldanga.


输出:
14

时间复杂度: O(N 3 )
辅助空间: O(1)

高效的方法:通过使用排序和两个指针算法可以有效地解决该问题。
请按照以下步骤解决问题:

  • 以升序对给定数组arr []进行排序。
  • 运行从b = 0到数组大小的循环。
    1. 分别使用0和等于-1的数组大小初始化两个变量ac
    2. 成立的情况下运行循环,并检查以下条件:
      1. 如果a = b ,则增加a并继续循环。
      2. 如果c = b ,则递减c并继续循环。
      3. 如果arr [a] * arr [C]≤d ,则检查以下条件:
        • 如果a ,则将它们之间的元素数量增加计数– 1( arr [b]除外)。
        • 否则,请增加它们之间元素的数量。
      4. 否则,递减c
  • 对于每对, ac可能有两个值。因此返回计数* 2

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the number of triplets
// (a, b, c) such that the equation
// ax^2 + bx + c = 0 has real roots
int getCount(int arr[], int N)
{
    // Sort the array in
    // ascending order
    sort(arr, arr + N);
 
    // Stores count of triplets(a, b, c) such
    // that ax^2 + bx + c = 0 has real roots
    int count = 0;
     
    // Base case
    if (N < 3)
        return 0;
 
    // Traverse the given array
    for (int b = 0; b < N; b++) {
 
        int a = 0, c = N - 1;
        int d = arr[b] * arr[b] / 4;
 
        while (a < c) {
 
            // If values of a and
            // c are equal to b
            if (a == b) {
                 
                // Increment a
                a++;
                continue;
            }
            if (c == b) {
                 
                // Decrement c
                c--;
                continue;
            }
 
            // Condition for having real
            // roots for a quadratic equation
            if (arr[a] * arr <= d) {
 
                // If b lies in
                // between a and c
                if (a < b && b < c) {
                     
                     
                    // Update count
                    count += c - a - 1;
                }
                else {
                     
                    // Update count
                    count += c - a;
                }
                 
                // Increment a
                a++;
            }
            else {
                 
                // Decrement c
                c--;
            }
        }
    }
 
    // For each pair two values
    // are possible of a and c
    return count * 2;
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 6, 10, 13, 21 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << getCount(arr, N);
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to count the number of triplets
// (a, b, c) such that the equation
// ax^2 + bx + c = 0 has real roots
static int getCount(int arr[], int N)
{
   
    // Sort the array in
    // ascending order
    Arrays.sort(arr);
 
    // Stores count of triplets(a, b, c) such
    // that ax^2 + bx + c = 0 has real roots
    int count = 0;
     
    // Base case
    if (N < 3)
        return 0;
 
    // Traverse the given array
    for (int b = 0; b < N; b++)
    {
        int a = 0, c = N - 1;
        int d = arr[b] * arr[b] / 4;
        while (a < c)
        {
 
            // If values of a and
            // c are equal to b
            if (a == b)
            {
                 
                // Increment a
                a++;
                continue;
            }
            if (c == b)
            {
                 
                // Decrement c
                c--;
                continue;
            }
 
            // Condition for having real
            // roots for a quadratic equation
            if (arr[a] * arr <= d)
            {
 
                // If b lies in
                // between a and c
                if (a < b && b < c)
                {
                       
                    // Update count
                    count += c - a - 1;
                }
                else
                {
                     
                    // Update count
                    count += c - a;
                }
                 
                // Increment a
                a++;
            }
            else {
                 
                // Decrement c
                c--;
            }
        }
    }
 
    // For each pair two values
    // are possible of a and c
    return count * 2;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 3, 6, 10, 13, 21 };
    int N = arr.length;
 
    // Function Call
    System.out.print(getCount(arr, N));
}
}
 
// This code is contributed by code_hunt.

Python3

# Python3 Program for the above approach
 
# Function to  count the number of triplets(a,b,c)
# Such that the equations ax^2 + bx + c = 0
# has real roots
def getcount(arr, N):
 
    # sort he array in
    # ascending order
    arr.sort()
 
    # store count of triplets (a,b,c) such
    # that ax^2 + bx + c = 0 has real roots
    count = 0
 
    # base case
    if (N < 3):
        return 0
       
    # Traverse the given array
    for b in range(0, N):
        a = 0
        c = N - 1
        d = arr[b] * arr[b] // 4
        while(a < c):
           
            # if value of a and
            # c are equal to b
            if (a == b):
               
                # increment a
                a += 1
                continue
            if (c == b):
               
                # Decrement c
                c -= 1
                continue
                 
            # condition for having real
            # roots for a quadratic equation
            if (arr[a] * arr) <= d:
               
                # if b lies in
                # between a and c
                if (a < b and b < c):
                   
                    # update count
                    count += c - a - 1
                else:
                   
                    # update count
                    count += c - a
                     
                # increment a
                a += 1
            else:
               
                # Decrement c
                c -= 1
                 
    # for each pair two values
    # are possible of a and c           
    return count * 2
 
# Driver code
arr = [3,6,10,13,21]
N = len(arr)
print(getcount(arr,N))
 
# This code is contributed by Virusbuddah

C#

// C# program for the above approach
using System;
 
class GFG{
 
  // Function to count the number of triplets
  // (a, b, c) such that the equation
  // ax^2 + bx + c = 0 has real roots
  static int getCount(int[] arr, int N)
  {
 
    // Sort the array in
    // ascending order
    Array.Sort(arr);
 
    // Stores count of triplets(a, b, c) such
    // that ax^2 + bx + c = 0 has real roots
    int count = 0;
 
    // Base case
    if (N < 3)
      return 0;
 
    // Traverse the given array
    for (int b = 0; b < N; b++)
    {
      int a = 0, c = N - 1;
      int d = arr[b] * arr[b] / 4;
      while (a < c)
      {
 
        // If values of a and
        // c are equal to b
        if (a == b)
        {
 
          // Increment a
          a++;
          continue;
        }
        if (c == b)
        {
 
          // Decrement c
          c--;
          continue;
        }
 
        // Condition for having real
        // roots for a quadratic equation
        if (arr[a] * arr <= d)
        {
 
          // If b lies in
          // between a and c
          if (a < b && b < c)
          {
 
            // Update count
            count += c - a - 1;
          }
          else
          {
 
            // Update count
            count += c - a;
          }
 
          // Increment a
          a++;
        }
        else {
 
          // Decrement c
          c--;
        }
      }
    }
 
    // For each pair two values
    // are possible of a and c
    return count * 2;
  }
 
 
  // Driver Code
  static public void Main()
  {
    int[] arr = { 3, 6, 10, 13, 21 };
    int N = arr.Length;
 
    // Function Call
    Console.Write(getCount(arr, N));
  }
}
 
// This code is contributed by susmitakundugoaldanga.
输出:
16

时间复杂度: O(N 2 )
辅助空间: O(1)