📌  相关文章
📜  检查数组的每个元素是否是另一个数组的任意两个元素的总和

📅  最后修改于: 2021-10-27 06:55:26             🧑  作者: Mango

给定两个由N 个整数组成的数组A[]B[] ,任务是检查数组B[] 的每个元素是否可以通过添加数组A[] 的任意两个元素来形成。如果可能,则打印“是” 。否则,打印“否”

例子:

方法:
请按照以下步骤解决问题:

  • B[] 的每个元素存储在一个集合中。
  • 对于数组 A[] 的每一对索引(i, j) ,检查集合中是否存在A[i] + A[j] 。如果发现为真,则从集合中移除A[i] + A[j]
  • 如果集合变为空,则打印“是” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to check if each element
// of B[] can be formed by adding two
// elements of array A[]
string checkPossible(int A[], int B[], int n)
{
    // Store each element of B[]
    unordered_set values;
 
    for (int i = 0; i < n; i++) {
        values.insert(B[i]);
    }
 
    // Traverse all possible pairs of array
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
 
            // If A[i] + A[j] is present in
            // the set
            if (values.find(A[i] + A[j])
                != values.end()) {
 
                // Remove A[i] + A[j] from the set
                values.erase(A[i] + A[j]);
 
                if (values.empty())
                    break;
            }
        }
    }
 
    // If set is empty
    if (values.size() == 0)
        return "Yes";
 
    // Otherwise
    else
        return "No";
}
 
// Driver Code
int main()
{
    int N = 5;
 
    int A[] = { 3, 5, 1, 4, 2 };
    int B[] = { 3, 4, 5, 6, 7 };
 
    cout << checkPossible(A, B, N);
}


Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
 
class GFG{
     
// Function to check if each element
// of B[] can be formed by adding two
// elements of array A[]
static String checkPossible(int A[], int B[],
                            int n)
{
     
    // Store each element of B[]
    Set values = new HashSet();
 
    for(int i = 0; i < n; i++)
    {
        values.add(B[i]);
    }
 
    // Traverse all possible pairs of array
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
 
            // If A[i] + A[j] is present in
            // the set
            if (values.contains(A[i] + A[j]))
            {
                 
                // Remove A[i] + A[j] from the set
                values.remove(A[i] + A[j]);
 
                if (values.size() == 0)
                    break;
            }
        }
    }
 
    // If set is empty
    if (values.size() == 0)
        return "Yes";
 
    // Otherwise
    else
        return "No";
}
 
// Driver Code
public static void main(String args[])
{
    int N = 5;
    int A[] = { 3, 5, 1, 4, 2 };
    int B[] = { 3, 4, 5, 6, 7 };
     
    System.out.print(checkPossible(A, B, N));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to implement
# the above approach
 
# Function to check if each element
# of B[] can be formed by adding two
# elements of array A[]
def checkPossible(A, B, n):
 
    # Store each element of B[]
    values = set([])
 
    for i in range (n):
        values.add(B[i])
     
    # Traverse all possible
    # pairs of array
    for i in range (n):
        for j in range (n):
 
            # If A[i] + A[j] is present in
            # the set
            if ((A[i] + A[j]) in values):
 
                # Remove A[i] + A[j] from the set
                values.remove(A[i] + A[j])
 
                if (len(values) == 0):
                    break
 
    # If set is empty
    if (len(values) == 0):
        return "Yes"
 
    # Otherwise
    else:
        return "No"
 
# Driver Code
if __name__ == "__main__":
   
  N = 5
 
  A = [3, 5, 1, 4, 2]
  B = [3, 4, 5, 6, 7]
 
  print (checkPossible(A, B, N))
 
# This code is contributed by Chitranayal


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
     
// Function to check if each element
// of []B can be formed by adding two
// elements of array []A
static String checkPossible(int []A, int []B,
                            int n)
{
 
  // Store each element of []B
  HashSet values = new HashSet();
 
  for(int i = 0; i < n; i++)
  {
    values.Add(B[i]);
  }
 
  // Traverse all possible pairs of array
  for(int i = 0; i < n; i++)
  {
    for(int j = 0; j < n; j++)
    {
      // If A[i] + A[j] is present in
      // the set
      if (values.Contains(A[i] + A[j]))
      {                
        // Remove A[i] + A[j] from the set
        values.Remove(A[i] + A[j]);
 
        if (values.Count == 0)
          break;
      }
    }
  }
 
  // If set is empty
  if (values.Count == 0)
    return "Yes";
 
  // Otherwise
  else
    return "No";
}
 
// Driver Code
public static void Main(String []args)
{
  int N = 5;
  int []A = {3, 5, 1, 4, 2};
  int []B = {3, 4, 5, 6, 7};
 
  Console.Write(checkPossible(A, B, N));
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
Yes

时间复杂度: O(N 2 )
辅助空间: O(N)

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