Check if each element of an Array is the Sum of any two elements of another Array
Last Updated :
06 Jun, 2021
Given two arrays A[] and B[] consisting of N integers, the task is to check if each element of array B[] can be formed by adding any two elements of array A[]. If it is possible, then print “Yes”. Otherwise, print “No”.
Examples:
Input: A[] = {3, 5, 1, 4, 2}, B[] = {3, 4, 5, 6, 7}
Output: Yes
Explanation:
B[0] = 3 = (1 + 2) = A[2] + A[4],
B[1] = 4 = (1 + 3) = A[2] + A[0],
B[2] = 5 = (3 + 2) = A[0] + A[4],
B[3] = 6 = (2 + 4) = A[4] + A[3],
B[4] = 7 = (3 + 4) = A[0] + A[3]
Input: A[] = {1, 2, 3, 4, 5}, B[] = {1, 2, 3, 4, 5}
Output: No
Approach:
Follow the steps below to solve the problem:
- Store each element of B[] in a Set.
- For each pair of indices (i, j) of the array A[], check if A[i] + A[j] is present in the set. If found to be true, remove A[i] + A[j] from the set.
- If the set becomes empty, then print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
#include
using namespace std;
string checkPossible( int A[], int B[], int n)
{
unordered_set values;
for ( int i = 0; i < n; i++) {
values.insert(B[i]);
}
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
if (values.find(A[i] + A[j])
!= values.end()) {
values.erase(A[i] + A[j]);
if (values.empty())
break ;
}
}
}
if (values.size() == 0)
return "Yes" ;
else
return "No" ;
}
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
import java.io.*;
import java.util.*;
class GFG{
static String checkPossible( int A[], int B[],
int n)
{
Set values = new HashSet();
for ( int i = 0 ; i < n; i++)
{
values.add(B[i]);
}
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
{
if (values.contains(A[i] + A[j]))
{
values.remove(A[i] + A[j]);
if (values.size() == 0 )
break ;
}
}
}
if (values.size() == 0 )
return "Yes" ;
else
return "No" ;
}
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));
}
}
|
Python3
def checkPossible(A, B, n):
values = set ([])
for i in range (n):
values.add(B[i])
for i in range (n):
for j in range (n):
if ((A[i] + A[j]) in values):
values.remove(A[i] + A[j])
if ( len (values) = = 0 ):
break
if ( len (values) = = 0 ):
return "Yes"
else :
return "No"
if __name__ = = "__main__" :
N = 5
A = [ 3 , 5 , 1 , 4 , 2 ]
B = [ 3 , 4 , 5 , 6 , 7 ]
print (checkPossible(A, B, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static String checkPossible( int []A, int []B,
int n)
{
HashSet values = new HashSet();
for ( int i = 0; i < n; i++)
{
values.Add(B[i]);
}
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
if (values.Contains(A[i] + A[j]))
{
values.Remove(A[i] + A[j]);
if (values.Count == 0)
break ;
}
}
}
if (values.Count == 0)
return "Yes" ;
else
return "No" ;
}
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));
}
}
|
Javascript
<script>
function checkPossible(A, B, n)
{
var values = new Set();
for ( var i = 0; i < n; i++)
{
values.add(B[i]);
}
for ( var i = 0; i < n; i++)
{
for ( var j = 0; j < n; j++)
{
if (values.has(A[i] + A[j]))
{
values. delete (A[i] + A[j]);
if (values.size == 0)
break ;
}
}
}
if (values.size == 0)
return "Yes" ;
else
return "No" ;
}
var N = 5;
var A = [ 3, 5, 1, 4, 2 ];
var B = [ 3, 4, 5, 6, 7 ];
document.write(checkPossible(A, B, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...