📜  从两个数组中计算对,其模运算产生 K

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

给定一个整数k 和两个数组arr1 arr2 ,任务是计算总对数(在从中选择一个元素后形成的arr1 另一个来自arr2 ) 从这些数组中取模运算产生k .
注意:如果在一对(a, b) 中a > b则模数必须作为a % b执行。此外,出现多次的对将只计算一次。
例子:

方法:

  • 取一个元素arr1 一次并与所有其他元素执行它的模运算arr2 逐个。
  • 如果上一步的结果等于k 然后将一对 (a, b) 存储在一个集合中以避免重复,其中 a 是较小的元素,而 b 是较大的元素。
  • 所需的总对数将是最终集合的大小。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function to return the total pairs
// of elements whose modulo yield K
int totalPairs(int arr1[], int arr2[], int K, int n, int m)
{
 
    // set is used to avoid duplicate pairs
    set > s;
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
 
            // check which element is greater and
            // proceed according to it
            if (arr1[i] > arr2[j]) {
 
                // check if modulo is equal to K
                if (arr1[i] % arr2[j] == K)
                    s.insert(make_pair(arr1[i], arr2[j]));
            }
            else {
                if (arr2[j] % arr1[i] == K)
                    s.insert(make_pair(arr2[j], arr1[i]));
            }
        }
    }
 
    // return size of the set
    return s.size();
}
 
// Driver code
int main()
{
    int arr1[] = { 8, 3, 7, 50 };
    int arr2[] = { 5, 1, 10, 4 };
    int K = 3;
    int n = sizeof(arr1) / sizeof(arr1[0]);
    int m = sizeof(arr2) / sizeof(arr2[0]);
 
    cout << totalPairs(arr1, arr2, K, n, m);
    return 0;
}


Java
// Java implementation of above approach
import java.util.*;
 
class GFG
{
    static class pair
    {
        int first, second;
        public pair(int first, int second)
        {
            this.first = first;
            this.second = second;
        }
    }
     
    // Function to return the total pairs
    // of elements whose modulo yield K
    static int totalPairs(int []arr1, int []arr2,
                          int K, int n, int m)
    {
     
        // set is used to avoid duplicate pairs
        HashSet s = new HashSet();
     
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
     
                // check which element is greater and
                // proceed according to it
                if (arr1[i] > arr2[j])
                {
     
                    // check if modulo is equal to K
                    if (arr1[i] % arr2[j] == K)
                        s.add(new pair(arr1[i], arr2[j]));
                }
                else
                {
                    if (arr2[j] % arr1[i] == K)
                        s.add(new pair(arr2[j], arr1[i]));
                }
            }
        }
     
        // return size of the set
        return s.size();
    }
     
    // Driver code
    public static void main(String []args)
    {
        int []arr1 = { 8, 3, 7, 50 };
        int []arr2 = { 5, 1, 10, 4 };
        int K = 3;
        int n = arr1.length;
        int m = arr2.length;
     
        System.out.println(totalPairs(arr1, arr2, K, n, m));
    }
}
 
// This code is contributed by Princi Singh


Python3
# Python3 implementation of above approach
 
# Function to return the total pairs
# of elements whose modulo yield K
def totalPairs(arr1, arr2, K, n, m):
 
    # set is used to avoid duplicate pairs
    s={}
 
    for i in range(n):
        for j in range(m):
 
            # check which element is greater and
            # proceed according to it
            if (arr1[i] > arr2[j]):
 
                # check if modulo is equal to K
                if (arr1[i] % arr2[j] == K):
                    s[(arr1[i], arr2[j])]=1
            else:
                if (arr2[j] % arr1[i] == K):
                    s[(arr2[j], arr1[i])]=1
 
 
 
    # return size of the set
    return len(s)
 
# Driver code
 
arr1 = [ 8, 3, 7, 50 ]
arr2 = [5, 1, 10, 4 ]
K = 3
n = len(arr1)
m = len(arr2)
 
print(totalPairs(arr1, arr2, K, n, m))
 
# This code is contributed by mohit kumar 29


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
     
class GFG
{
    public class pair
    {
        public int first, second;
        public pair(int first, int second)
        {
            this.first = first;
            this.second = second;
        }
    }
     
    // Function to return the total pairs
    // of elements whose modulo yield K
    static int totalPairs(int []arr1, int []arr2,
                          int K, int n, int m)
    {
     
        // set is used to avoid duplicate pairs
        HashSet s = new HashSet();
     
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
     
                // check which element is greater and
                // proceed according to it
                if (arr1[i] > arr2[j])
                {
     
                    // check if modulo is equal to K
                    if (arr1[i] % arr2[j] == K)
                        s.Add(new pair(arr1[i], arr2[j]));
                }
                else
                {
                    if (arr2[j] % arr1[i] == K)
                        s.Add(new pair(arr2[j], arr1[i]));
                }
            }
        }
     
        // return size of the set
        return s.Count;
    }
     
    // Driver code
    public static void Main(String []args)
    {
        int []arr1 = { 8, 3, 7, 50 };
        int []arr2 = { 5, 1, 10, 4 };
        int K = 3;
        int n = arr1.Length;
        int m = arr2.Length;
     
        Console.WriteLine(totalPairs(arr1, arr2, K, n, m));
    }
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
3

注意:要打印所有对,只需打印集合的元素。

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