📌  相关文章
📜  计算三元组,使任意两个数的和等于第三个 | 2套

📅  最后修改于: 2021-10-28 01:29:19             🧑  作者: Mango

给定一个长度为N不同正整数arr[]的数组,任务是计算所有三元组,使得两个元素的总和等于第三个元素。

例子:

方法:这个想法是创建一个数组中存在的数字的频率数组,然后在 O 中的频率数组的帮助下检查每对元素的对元素的总和是否存在于数组中(1次。
算法:

  • 声明一个频率数组来存储数字的频率。
  • 迭代数组的元素并增加频率数组中该数字的计数。
  • 运行两个循环以选择矩阵的两个不同索引,并检查这些索引处元素的总和在频率数组中的频率是否大于 0。
If frequency of the sum is greater than 0:
    Increment the count of the triplets.

注意:我们在程序中假设数组元素的值在[1, 100]范围内。
下面是上述方法的实现:

C++
// C++ implementation to count the
// triplets such that the sum of the
// two numbers is equal to third number
 
#include 
using namespace std;
 
// Function to find the count of the
// triplets such that sum of two
// numbers is equal to the third number
int countTriplets(int arr[], int n){
    int freq[100] = {0};
     
    // Loop to count the frequency
    for (int i=0; i < n; i++){
        freq[arr[i]]++;
    }
    int count = 0;
     
    // Loop to count for triplets
    for(int i = 0;i < n; i++){
        for(int j = i+1; j < n; j++){
            if(freq[arr[i] + arr[j]]){
                count++;
            }
        }
    }
    return count;
}
 
// Driver Code
int main()
{
    int n = 4;
    int arr[] = {1, 5, 3, 2};
     
    // Function Call
    cout << countTriplets(arr, n);
    return 0;
}


Java
// Java implementation to count the
// triplets such that the sum of the
// two numbers is equal to third number
class GFG{
 
// Function to find the count of the
// triplets such that sum of two
// numbers is equal to the third number
static int countTriplets(int arr[], int n){
    int []freq = new int[100];
     
    // Loop to count the frequency
    for (int i = 0; i < n; i++){
        freq[arr[i]]++;
    }
    int count = 0;
     
    // Loop to count for triplets
    for(int i = 0; i < n; i++){
        for(int j = i + 1; j < n; j++){
            if(freq[arr[i] + arr[j]] > 0){
                count++;
            }
        }
    }
    return count;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 4;
    int arr[] = {1, 5, 3, 2};
     
    // Function Call
    System.out.print(countTriplets(arr, n));
}
}
 
// This code is contributed by Rajput-Ji


Python 3
# Python 3 implementation to count the
# triplets such that the sum of the
# two numbers is equal to third number
 
# Function to find the count of the
# triplets such that sum of two
# numbers is equal to the third number
def countTriplets(arr, n):
    freq = [0 for i in range(100)]
     
    # Loop to count the frequency
    for i in range(n):
        freq[arr[i]] += 1
    count = 0
     
    # Loop to count for triplets
    for i in range(n):
        for j in range(i + 1, n, 1):
            if(freq[arr[i] + arr[j]]):
                count += 1
    return count
 
# Driver Code
if __name__ == '__main__':
    n = 4
    arr = [1, 5, 3, 2]
     
    # Function Call
    print(countTriplets(arr, n))
 
# This code is contributed by Surendra_Gangwar


C#
// C# implementation to count the
// triplets such that the sum of the
// two numbers is equal to third number
using System;
 
class GFG{
 
// Function to find the count of the
// triplets such that sum of two
// numbers is equal to the third number
static int countTriplets(int []arr, int n){
    int []freq = new int[100];
     
    // Loop to count the frequency
    for (int i = 0; i < n; i++){
        freq[arr[i]]++;
    }
    int count = 0;
     
    // Loop to count for triplets
    for(int i = 0; i < n; i++){
        for(int j = i + 1; j < n; j++){
            if(freq[arr[i] + arr[j]] > 0){
                count++;
            }
        }
    }
    return count;
}
 
// Driver Code
public static void Main(string[] args)
{
    int n = 4;
    int []arr = {1, 5, 3, 2};
     
    // Function Call
    Console.WriteLine(countTriplets(arr, n));
}
}
 
// This code is contributed by Yahs_R


Javascript


输出:
2

性能分析:

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程