算法:2 个集合 A 和 B 形成和集 C给定 A 和 C 找到 B

问题描述

2 个集合 A 和 B 形成一个和集合 C,这意味着 S(A,B) = {a+b 其中 a 属于 A,b 属于 B} 给定 C 和 A,我们需要找到 B。 see here

所以我采用了 ai - cj = m 元素的方法,如果在 ai 的所有迭代中找到 m 个元素,则添加到 B

这里解释得更好

输入
a = {1 2}
x = {3 4 5}
输出
负值 a=1 c=3 匹配 2 和匹配值 2
负值 a=2 c=4 匹配 2 和匹配值 2
匹配值 2
负值 a=1 c=4 匹配 3 和匹配值 3
负值 a=2 c=5 匹配 3 和匹配值 3
匹配值 3
负值 a=1 c=5 匹配 4 和匹配值 4

result = 2,3 这是正确的

但是对于一些测试用例来说它失败了

喜欢

8 12 15 18 24 30 33 40 41 48

3 5 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 94 97 

这个测试用例失败了。预期输出

1 3 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 34 3 4 3 4 35 34 3 4 3 4

我的输出

1 3 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 34 4 3 4 3 7 35 36 4 3 4 3 4 9

45 这是额外的元素

代码

import java.util.*;
class TestClass {
    public static void main(String args[] ) throws Exception {
    
    Scanner sc = new Scanner(system.in);

    int n = sc.nextInt();
    int[] a = new int[n];
    for(int i=0;i<n;i++)
        a[i] = sc.nextInt();
    int m = sc.nextInt();
    int[] c= new int[m];
    for(int i=0;i<m;i++)
        c[i] = sc.nextInt();
    
    Arrays.sort(a);
    Arrays.sort(c);
    int b[] = new int[m];
    int k=0;
    int v = 0;
    while(v!=c.length)
    {
        int flag=0;
        int match = (a[0]>c[v])? a[0]-c[v] : c[v]-a[0]; //since a[0] is the first iteration all of the common elements must be present here
        for(int i=1;i<n;i++) 
        {
            flag=0;
            for(int j=0;j<m;j++)
            {
                int minus = (a[i]>c[j])? a[i]-c[j] : c[j]-a[i];
               
                if(minus == match){
                   //  System.out.println("Minus Value initial a "+a[i]+" c "+c[j]+" match "+ minus+ " and the match value "+ match);
                    flag = 1;
                    break;}
            }
            if(flag==0)
                break;
        }
        if(flag==1){
         //   System.out.println("match value "+ match);
        b[k++] = match;
            }
        v++;
    }

    for(int i=0;i<k;i++)
        System.out.print(b[i]+" ");

    }
}

请看一看,让我知道我的算法有什么问题或者是否有更好的方法

解决方法

因此,首先排序不会增加太多价值,因为您无论如何都使用 2 个嵌套的 for 循环。

接下来,a 是一个正数组。 a[i] - c[i] 没有意义,因为 c[i] 总是大于 a[i]。

您是说只有在所有迭代中都找到它时才需要将其添加到答案中。

但是,我不认为你的代码会那样做。

你的基本想法是正确的。

如果 a + b =c -> b = (a-c)

所以,这就是我将如何解决这个问题。相同的想法,但维护地图。

现在,您需要在这里做的是维护 c 和 a 之间所有差异计数的映射。

如果这个计数与 a 的长度匹配,那么你得到了匹配,输出它。

一个简单的代码是:

Map<Integer,Integer> map = new TreeMap<>();

for(int v: c){
   for(int u: a){
      int diff = v-u;
      map.put(diff,map.getOrDefault(diff,0)+1);
   }
}

        
for(int i: map.keySet()){
   if(map.get(i)==a.length){
      System.out.print(i+" ");
   }
}