📌  相关文章
📜  两个数组之间的对数,使得总和不同

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

给定两个数组a[]b[] ,任务是找到所有对(a[i], b[j])的计数,使得a[i] + b[j]在所有对中是唯一的,即如果两对的总和相等,则结果中只会计算一对。
例子:

方法:初始化count = 0 ,运行两个循环,考虑所有可能的对,将每对的和存储在unordered_set中,以检查之前是否已经获得了和。如果它有然后忽略当前对否则增加计数
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count
// of pairs with distinct sum
int countPairs(int a[], int b[], int n, int m)
{
 
    // To store the required count
    int cnt = 0;
 
    // Set to store the sum
    // obtained for each pair
    unordered_set s;
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
 
            // Sum of the current pair
            int sum = a[i] + b[j];
 
            // If the sum obtained is distinct
            if (s.count(sum) == 0) {
 
                // Increment the count
                cnt++;
 
                // Insert sum in the set
                s.insert(sum);
            }
        }
    }
 
    return cnt;
}
 
// Driver code
int main()
{
    int a[] = { 12, 2, 7 };
    int n = sizeof(a) / sizeof(a[0]);
    int b[] = { 4, 3, 8 };
    int m = sizeof(b) / sizeof(b[0]);
 
    cout << countPairs(a, b, n, m);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
     
    // Function to return the count
    // of pairs with distinct sum
    static int countPairs(int a[], int b[], int n, int m)
    {
     
        // To store the required count
        int cnt = 0;
     
        // Set to store the sum
        // obtained for each pair
        HashSet s = new HashSet();
 
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
     
                // Sum of the current pair
                int sum = a[i] + b[j];
     
                // If the sum obtained is distinct
                if (s.contains(sum) == false)
                {
     
                    // Increment the count
                    cnt++;
     
                    // Insert sum in the set
                    s.add(sum);
                }
            }
        }
     
        return cnt;
    }
     
    // Driver code
    static public void main (String args[])
    {
        int a[] = { 12, 2, 7 };
        int n = a.length;
        int b[] = { 4, 3, 8 };
        int m = b.length;
     
        System.out.println(countPairs(a, b, n, m));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
 
# Function to return the count
# of pairs with distinct sum
def countPairs(a, b, n, m):
 
    # To store the required count
    cnt = 0
 
    # Set to store the sum
    # obtained for each pair
    s=dict()
 
    for i in range(n):
        for j in range(m):
 
            # Sum of the current pair
            sum = a[i] + b[j]
 
            # If the sum obtained is distinct
            if (sum not in s.keys()):
                # Increment the count
                cnt+=1
 
                # Insert sum in the set
                s[sum]=1
 
    return cnt
 
 
# Driver code
 
a =[ 12, 2, 7]
n = len(a)
b =[ 4, 3, 8 ]
m = len(b)
 
print(countPairs(a, b, n, m))
 
# This code is contributed by mohit kumar 29


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
     
    // Function to return the count
    // of pairs with distinct sum
    static int countPairs(int []a, int []b,
                          int n, int m)
    {
     
        // To store the required count
        int cnt = 0;
     
        // Set to store the sum
        // obtained for each pair
        HashSet s = new HashSet();
 
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
     
                // Sum of the current pair
                int sum = a[i] + b[j];
     
                // If the sum obtained is distinct
                if (s.Contains(sum) == false)
                {
     
                    // Increment the count
                    cnt++;
     
                    // Insert sum in the set
                    s.Add(sum);
                }
            }
        }
     
        return cnt;
    }
     
    // Driver code
    static public void Main (String []args)
    {
        int []a = { 12, 2, 7 };
        int n = a.Length;
        int []b = { 4, 3, 8 };
        int m = b.Length;
     
        Console.WriteLine(countPairs(a, b, n, m));
    }
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:

7

时间复杂度: O(N * M)。
辅助空间:O(1)。

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