Count of pairs between two arrays such that the sums are distinct

Given two arrays a[] and b[], the task is to find the count of all pairs (a[i], b[j]) such that a[i] + b[j] is unique among all the pairs i.e. if two pairs have equal sum then only one will be counted in the result.
Examples:Â
Â
Input: a[] = {3, 3}, b[] = {3}Â
Output: 1Â
The two possible pairs are (a[0], b[0]) and (a[1], b[0]).Â
Pair 1: 3 + 3 = 6Â
Pair 2: 3 + 3 = 6
Input: a[] = {12, 2, 7}, b[] = {4, 3, 8}Â
Output: 7Â
Â
Â
Approach: Initialise count = 0 and run two loops to consider all possible pairs and store the sum of every pair in an unordered_set to check whether the sum has been obtained before. If it has then ignore the current pair else increment the count.
Below is the implementation of the above approach:Â
Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to return the count// of pairs with distinct sumint 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<int> 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 codeint 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<Integer> s = new HashSet<Integer>();Â
        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 sumdef 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<int> s = new HashSet<int>();Â
        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
<script>Â
    // Javascript implementation of the approachÂ
    // Function to return the count     // of pairs with distinct sum     function countPairs(a, b, n, m)     {                // To store the required count         let cnt = 0;                // Set to store the sum         // obtained for each pair         let s = new Set();           for (let i = 0; i < n; i++)         {             for (let j = 0; j < m; j++)             {                        // Sum of the current pair                 let sum = a[i] + b[j];                        // If the sum obtained is distinct                 if (s.has(sum) == false)                 {                            // Increment the count                     cnt++;                            // Insert sum in the set                     s.add(sum);                 }             }         }                return cnt;     }         // Driver code             let a = [ 12, 2, 7 ];         let n = a.length;         let b = [ 4, 3, 8 ];         let m = b.length;                document.write(countPairs(a, b, n, m));              // This code is contributed by susmitakundugoaldanga.     </script> |
7
Â
Time complexity: O(N * M).
Auxiliary Space: O(1). Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



