Sudo Placement | Beautiful Pairs

Given two arrays of integers where maximum size of first array is big and that of second array is small. Your task is to find if there is a pair in the first array whose sum is present in the second array.

Examples:

Input: 
4
1 5 10 8
3
2 20 13
Output: 1

Approach : We have x + y = z. This can be rewritten as x = z – y. This means, we need to find an element x in array 1 such that it is the result of z(second array) – y(first array). For this, use hashing to keep track of such element x.

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP code for finding required pairs
#include <bits/stdc++.h>
using namespace std;
  
// The function to check if beautiful pair exists
bool pairExists(int arr1[], int m, int arr2[], int n)
{
    // Set for hashing
    unordered_set<int> s;
  
    // Traversing the first array
    for (int i = 0; i < m; i++) {
  
        // Traversing the second array to check for 
        // every j corresponding to single i
        for (int j = 0; j < n; j++) {
  
            // x + y = z => x = y - z
            if (s.find(arr2[j] - arr1[i]) != s.end())
  
                // if such x exists then we return true
                return true;
  
        }
  
       // hash to make use of it next time
       s.insert(arr1[i]);
 }
  
    // no pair exists
    return false;
}
  
// Driver Code
int main()
{
    int arr1[] = { 1, 5, 10, 8 };
    int arr2[] = { 2, 20, 13 };
  
    // If pair exists then 1 else 0
    // 2nd argument as size of first array
    // fourth argument as sizeof 2nd array
    if (pairExists(arr1, 4, arr2, 3))
        cout << 1 << endl;
    else
        cout << 0 << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for finding required pairs
import java.util.*;
class GFG
{
        // The function to check if beautiful pair exists
        static boolean pairExists(int []arr1, int m, int []arr2, int n)
        {
            // Set for hashing
            Set<Integer> s =new HashSet<Integer>();
          
            // Traversing the first array
            for (int i = 0; i < m; i++) {
          
                // Traversing the second array to check for 
                // every j corresponding to single i
                for (int j = 0; j < n; j++) 
                {
          
                    // x + y = z => x = y - z
                    if (s.contains(arr2[j] - arr1[i]))
          
                        // if such x exists then we return true
                        return true;
          
                }
          
            // hash to make use of it next time
            s.add(arr1[i]);
        }
          
            // no pair exists
            return false;
        }
          
        // Driver Code
        public static void main(String []args)
        {
            int []arr1 = { 1, 5, 10, 8 };
            int []arr2 = { 2, 20, 13 };
          
            // If pair exists then 1 else 0
            // 2nd argument as size of first array
            // fourth argument as sizeof 2nd array
            if (pairExists(arr1, 4, arr2, 3))
                System.out.println(1);
            else
                System.out.println(0);
          
        }
  
// This code is contributed by ihritik
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code for finding required pairs
using System;
using System.Collections.Generic;
  
class GFG
{
        // The function to check if
        // beautiful pair exists
        static bool pairExists(int []arr1,
                int m, int []arr2, int n)
        {
            // Set for hashing
            HashSet<int> s = new HashSet<int>();
          
            // Traversing the first array
            for (int i = 0; i < m; i++) 
            {
          
                // Traversing the second array to check for 
                // every j corresponding to single i
                for (int j = 0; j < n; j++) 
                {
          
                    // x + y = z => x = y - z
                    if (s.Contains(arr2[j] - arr1[i]))
          
                        // if such x exists then we return true
                        return true;
          
                }
          
            // hash to make use of it next time
            s.Add(arr1[i]);
        }
          
            // no pair exists
            return false;
        }
          
        // Driver Code
        public static void Main()
        {
            int []arr1 = { 1, 5, 10, 8 };
            int []arr2 = { 2, 20, 13 };
          
            // If pair exists then 1 else 0
            // 2nd argument as size of first array
            // fourth argument as sizeof 2nd array
            if (pairExists(arr1, 4, arr2, 3))
                Console.WriteLine(1);
            else
                Console.WriteLine(0);
        }
}
  
/* This code contributed by PrinciRaj1992 */

chevron_right


Output:

1


My Personal Notes arrow_drop_up

This article is contributed by Team GeeksforGeeks. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ihritik, princiraj1992