📌  相关文章
📜  在由两个数组之和形成的集合中找到第 N 个项目

📅  最后修改于: 2022-05-13 01:57:49.905000             🧑  作者: Mango

在由两个数组之和形成的集合中找到第 N 个项目

给定两个排序数组,我们可以得到一组和(从第一个元素中添加一个元素,从第二个元素中添加一个元素)。查找按排序顺序考虑的已形成集合的元素中的第 N 个元素。
注意:总和集应具有唯一元素。

例子:

Input: arr1[] = {1, 2}
        arr2[] = {3, 4}
        N = 3
Output: 6
We get following elements set of sums.
4(1+3), 5(2+3 or 1+4), 6(2+4)
Third element in above set is 6.

Input: arr1[] = { 1,3, 4, 8, 10} 
        arr2[] = {20, 22, 30, 40} 
        N = 4
Output: 25
We get following elements set of sums.
21(1+20), 23(1+22 or 20+3), 24(20+4), 25(22+3)...
Fourth element is 25.

提问:微软面试

方法:

  • 运行两个循环——一个用于第一个数组,第二个用于第二个数组。
  • 只需考虑每一对并将它们的总和存储在自平衡 BST 中(由 C++ 中的 set 和 map 实现)。
  • 我们在这里在 C++ 中使用 set,因为我们只需要查看元素是否存在,我们不需要键值对。
  • 遍历集合并返回集合中的第 N 个元素。

下面是上述方法的实现:

C++
// C++ program to find N'th element in a set formed
// by sum of two arrays
#include
using namespace std;
 
//Function to calculate the set of sums
int calculateSetOfSum(int arr1[], int size1, int arr2[],
                      int size2, int N)
{
    // Insert each pair sum into set. Note that a set
    // stores elements in sorted order and unique elements
    set s;
    for (int i=0 ; i < size1; i++)
        for (int j=0; j < size2; j++)
            s.insert(arr1[i]+arr2[j]);
 
    // If set has less than N elements
    if (s.size() < N)
       return -1;
 
    // Find N'tb item in set and return it
    set::iterator it = s.begin();
    for (int count=1; count


Java
// Java program to find N'th element in a set formed
// by sum of two arrays
import java.util.*;
 
class GFG
{
 
// Function to calculate the set of sums
static int calculateSetOfSum(int arr1[], int size1, int arr2[],
                                            int size2, int N)
{
    // Insert each pair sum into set. Note that a set
    // stores elements in sorted order and unique elements
    SortedSet s = new TreeSet();
    for (int i = 0; i < size1; i++)
        for (int j = 0; j < size2; j++)
            s.add(arr1[i]+arr2[j]);
 
    // If set has less than N elements
    if (s.size() < N)
    return -1;
 
    // Find N'tb item in set and return it
     
    return (int)s.toArray()[ N-1 ];
}
 
// Driver code
public static void main(String[] args)
{
    int arr1[] = {1, 2};
    int size1 = arr1.length;
    int arr2[] = {3, 4};
    int size2 = arr2.length;
 
    int N = 3;
    int res = calculateSetOfSum(arr1, size1, arr2, size2, N);
    if (res == -1)
        System.out.println("N'th term doesn't exists in set");
    else
        System.out.println("N'th element in the set of sums is "
            +res);
}
}
 
// This code is contributed by 29AjayKumar


C#
// C# program to find N'th element in
// a set formed by sum of two arrays
using System;
using System.Linq;
using System.Collections.Generic;
     
class GFG
{
 
// Function to calculate the set of sums
static int calculateSetOfSum(int []arr1, int size1,
                             int []arr2, int size2,
                             int N)
{
    // Insert each pair sum into set.
    // Note that a set stores elements in
    // sorted order and unique elements
    HashSet s = new HashSet();
    for (int i = 0; i < size1; i++)
        for (int j = 0; j < size2; j++)
            s.Add(arr1[i] + arr2[j]);
 
    // If set has less than N elements
    if (s.Count < N)
    return -1;
 
    // Find N'tb item in set and return it
    int []last = s.ToArray();
    return last[s.Count - 1];
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr1 = {1, 2};
    int size1 = arr1.Length;
    int []arr2 = {3, 4};
    int size2 = arr2.Length;
 
    int N = 3;
    int res = calculateSetOfSum(arr1, size1,
                                arr2, size2, N);
    if (res == -1)
        Console.WriteLine("N'th term doesn't exists in set");
    else
        Console.WriteLine("N'th element in the set" +
                               " of sums is " + res);
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出
N'th element in the set of sums is 6

时间复杂度: O(mn log (mn)) 其中 m 是第一个数组的大小,n 是第二个数组的大小。