📜  满足方程式的六倍数(或六个值)的数量

📅  最后修改于: 2021-05-06 17:31:07             🧑  作者: Mango

给定n个元素的数组。任务是找到满足以下等式的六连体的数量,以使a,b,c,d,e和f属于给定数组:

a * b + c - e = f
    d

例子:

Input :  arr[] = { 1 }.
Output : 1
a = b = c = d = e = f = 1 satisfy
the equation.

Input :  arr[] = { 2, 3 }
Output : 4

Input :  arr[] = { 1, -1 }
Output : 24

首先,对方程重新排序,a * b + c =(f + e)* d。
现在,制作两个数组,一个用于方程式的LHS(左手边),另一个用于方程式的RHS(右手边)。在LHS数组中搜索RHS数组的每个元素。每当在LHS中找到RHS的值时,请检查它在LHS中重复了多少次,并将该计数加到总数中。通过对LHS数组进行排序,可以使用二进制搜索来进行搜索。

以下是此方法的实现:

C++
// C++ program to count of 6 values from an array
// that satisfy an equation with 6 variables
#include
using namespace std;
  
// Returns count of 6 values from arr[]
// that satisfy an equation with 6 variables
int findSextuplets(int arr[], int n)
{
    // Generating possible values of RHS of the equation
    int index = 0;
    int RHS[n*n*n + 1];
    for (int i = 0; i < n; i++)
      if (arr[i])  // Checking d should be non-zero.
        for (int j = 0; j < n; j++)
          for (int k = 0; k < n; k++)
            RHS[index++] = arr[i] * (arr[j] + arr[k]);
  
    // Sort RHS[] so that we can do binary search in it.
    sort(RHS, RHS + n);
  
    // Generating all possible values of LHS of the equation
    // and finding the number of occurances of the value in RHS.
    int result = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            for(int k = 0; k < n; k++)
            {
                int val = arr[i] * arr[j] + arr[k];
                result += (upper_bound(RHS, RHS + index, val) -
                          lower_bound(RHS, RHS + index, val));
            }
        }
    }
  
    return result;
}
  
// Driven Program
int main()
{
    int arr[] = {2, 3};
    int n = sizeof(arr)/sizeof(arr[0]);
  
    cout << findSextuplets(arr, n) << endl;
    return 0;
}


Java
// Java program to count of 6 values from an array 
// that satisfy an equation with 6 variables 
import java.util.Arrays; 
class GFG{
static int upper_bound(int[] array, int length, int value) {
        int low = 0;
        int high = length;
        while (low < high) {
            final int mid = (low + high) / 2;
            if (value >= array[mid]) {
                low = mid + 1;
            } else {
                high = mid;
            }
        }
        return low;
    }
  static int lower_bound(int[] array, int length, int value) {
        int low = 0;
        int high = length;
        while (low < high) {
            final int mid = (low + high) / 2;
            if (value <= array[mid]) {
                high = mid;
            } else {
                low = mid + 1;
            }
        }
        return low;
    }  
static int findSextuplets(int[] arr, int n)
{
    // Generating possible values of RHS of the equation 
    int index = 0;
    int[] RHS = new int[n * n * n + 1];
    for (int i = 0; i < n; i++)
    {
      if (arr[i] != 0) // Checking d should be non-zero.
      {
        for (int j = 0; j < n; j++)
        {
          for (int k = 0; k < n; k++)
          {
            RHS[index++] = arr[i] * (arr[j] + arr[k]);
          }
        }
      }
    }
  
    // Sort RHS[] so that we can do binary search in it. 
    Arrays.sort(RHS);
  
    // Generating all possible values of LHS of the equation 
    // and finding the number of occurances of the value in RHS. 
    int result = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            for (int k = 0; k < n; k++)
            {
                int val = arr[i] * arr[j] + arr[k];
                result += (upper_bound(RHS, index, val)-lower_bound(RHS, index, val));
            }
        }
    }
  
    return result;
}
  
// Driven Program 
public static void main(String[] args)
{
    int[] arr = {2, 3};
    int n = arr.length;
  
    System.out.println(findSextuplets(arr, n));
  
}
}
// This code is contributed by mits


Python3
# Python3 program to count of 6 values 
# from an array that satisfy an equation 
# with 6 variables 
  
def upper_bound(array, length, value):
    low = 0;
    high = length;
    while (low < high):
        mid = int((low + high) / 2);
        if (value >= array[mid]):
                low = mid + 1;
        else:
            high = mid;
          
    return low;
      
def lower_bound(array, length, value):
    low = 0;
    high = length;
    while (low < high):
        mid = int((low + high) / 2);
        if (value <= array[mid]):
            high = mid;
        else:
            low = mid + 1;
    return low;
      
def findSextuplets(arr, n):
  
    # Generating possible values of 
    # RHS of the equation 
    index = 0;
    RHS = [0] * (n * n * n + 1);
      
    for i in range(n):
        if (arr[i] != 0):
              
            # Checking d should be non-zero.
            for j in range(n):
                for k in range(n):
                    RHS[index] = arr[i] * (arr[j] + 
                                           arr[k]);
                    index += 1;
  
    # Sort RHS[] so that we can do
    # binary search in it. 
    RHS.sort();
  
    # Generating all possible values of 
    # LHS of the equation and finding the
    # number of occurances of the value in RHS. 
    result = 0;
    for i in range(n):
        for j in range(n):
            for k in range(n):
                val = arr[i] * arr[j] + arr[k];
                result += (upper_bound(RHS, index, val) -
                           lower_bound(RHS, index, val));
  
    return result;
  
# Driver Code
arr = [2, 3];
n = len(arr);
  
print(findSextuplets(arr, n));
  
# This code is contributed by mits


C#
// C# program to count of 6 values from an array 
// that satisfy an equation with 6 variables 
using System;
using System.Collections;
  
class GFG{
static int upper_bound(int[] array, int length, int value) {
        int low = 0;
        int high = length;
        while (low < high) {
            int mid = (low + high) / 2;
            if (value >= array[mid]) {
                low = mid + 1;
            } else {
                high = mid;
            }
        }
        return low;
    }
static int lower_bound(int[] array, int length, int value) {
        int low = 0;
        int high = length;
        while (low < high) {
            int mid = (low + high) / 2;
            if (value <= array[mid]) {
                high = mid;
            } else {
                low = mid + 1;
            }
        }
        return low;
    } 
static int findSextuplets(int[] arr, int n)
{
    // Generating possible values of RHS of the equation 
    int index = 0;
    int[] RHS = new int[n * n * n + 1];
    for (int i = 0; i < n; i++)
    {
    if (arr[i] != 0) // Checking d should be non-zero.
    {
        for (int j = 0; j < n; j++)
        {
        for (int k = 0; k < n; k++)
        {
            RHS[index++] = arr[i] * (arr[j] + arr[k]);
        }
        }
    }
    }
  
    // Sort RHS[] so that we can do binary search in it. 
    Array.Sort(RHS);
  
    // Generating all possible values of LHS of the equation 
    // and finding the number of occurances of the value in RHS. 
    int result = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            for (int k = 0; k < n; k++)
            {
                int val = arr[i] * arr[j] + arr[k];
                result += (upper_bound(RHS, index, val)-lower_bound(RHS, index, val));
            }
        }
    }
  
    return result;
}
  
// Driven Program 
static void Main()
{
    int[] arr = {2, 3};
    int n = arr.Length;
  
    Console.WriteLine(findSextuplets(arr, n));
  
}
}
// This code is contributed by mits


PHP
= $array[$mid])
                $low = $mid + 1;
        else
            $high = $mid;
    }
    return $low;
}
  
function lower_bound($array, $length, $value)
{
    $low = 0;
    $high = $length;
    while ($low < $high)
    {
        $mid = (int)(($low + $high) / 2);
        if ($value <= $array[$mid])
            $high = $mid;
        else
            $low = $mid + 1;
    }
    return $low;
}
  
// Returns count of 6 values from arr[]
// that satisfy an equation with 6 variables
function findSextuplets($arr, $n)
{
    // Generating possible values of
    // RHS of the equation 
    $index = 0;
    $RHS = array_fill(0, $n * $n * $n + 1, 0);
    for ($i = 0; $i < $n; $i++)
    if ($arr[$i] != 0) // Checking d should be non-zero.
        for ($j = 0; $j < $n; $j++)
        for ($k = 0; $k < $n; $k++)
            $RHS[$index++] = $arr[$i] * 
                            ($arr[$j] + $arr[$k]);
  
    // Sort RHS[] so that we can do
    // binary search in it. 
    sort($RHS);
  
    // Generating all possible values of LHS 
    // of the equation and finding the number
    // of occurances of the value in RHS. 
    $result = 0;
    for ($i = 0; $i < $n; $i++)
        for ($j = 0; $j < $n; $j++)
            for ($k = 0; $k < $n; $k++)
            {
                $val = $arr[$i] * $arr[$j] + $arr[$k];
                $result += (upper_bound($RHS, $index, $val) - 
                            lower_bound($RHS, $index, $val));
            }
  
    return $result;
}
  
// Driver Code
$arr = array(2, 3);
$n = count($arr);
  
print(findSextuplets($arr, $n));
  
// This code is contributed by mits
?>


输出:

4

时间复杂度: O(N 3 log N)