Choose two elements from the given array such that their sum is not present in any of the arrays

Given two arrays A[] and B[], the task is to choose two elements X and Y such that X belongs to A[] and Y belongs to B[] and (X + Y) must not be present in any of the array.
Examples:Â
Input: A[] = {3, 2, 2}, B[] = {1, 5, 7, 7, 9}Â
Output: 3 9Â
3 + 9 = 12 and 12 is not present inÂ
any of the given arrays.
Input: A[] = {1, 3, 5, 7}, B[] = {7, 5, 3, 1}Â
Output: 7 7Â
Approach: Choose X as the maximum element from A[] and Y as the maximum element from B[]. Now, it is obvious that (X + Y) will be greater than the maximum of both the arrays i.e. it will not be present in any of the arrays.
Below is the implementation of the above approach:Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to find the numbers from// the given arrays such that their// sum is not present in any// of the given arrayvoid findNum(int a[], int n, int b[], int m){    // Find the maximum element    // from both the arrays    int x = *max_element(a, a + n);    int y = *max_element(b, b + m);    cout << x << " " << y;}Â
// Driver codeint main(){Â Â Â Â int a[] = { 3, 2, 2 };Â Â Â Â int n = sizeof(a) / sizeof(int);Â Â Â Â int b[] = { 1, 5, 7, 7, 9 };Â Â Â Â int m = sizeof(b) / sizeof(int);Â
    findNum(a, n, b, m);    return 0;} |
Java
// Java implementation of the approachclass GFG{Â Â Â Â Â // find maximum element in an arraystatic int max_element(int a[], int n){Â Â Â Â int m = Integer.MIN_VALUE;Â Â Â Â Â Â Â Â Â for(int i = 0; i < n; i++)Â Â Â Â Â Â Â Â m = Math.max(m, a[i]);Â Â Â Â Â Â Â Â Â return m;}Â
// Function to find the numbers from// the given arrays such that their// sum is not present in any// of the given arraystatic void findNum(int a[], int n,                     int b[], int m){    // Find the maximum element    // from both the arrays    int x = max_element(a, n);    int y = max_element(b, m);    System.out.print(x + " " + y);}Â
// Driver codepublic static void main(String args[]){Â Â Â Â int a[] = { 3, 2, 2 };Â Â Â Â int n = a.length;Â Â Â Â int b[] = { 1, 5, 7, 7, 9 };Â Â Â Â int m = b.length;Â
    findNum(a, n, b, m);}}Â
// This code is contributed by Arnub Kundu |
Python3
# Python3 implementation of the approach Â
# Function to find the numbers from # the given arrays such that their # sum is not present in any # of the given array def findNum(a, n, b, m) :Â
    # Find the maximum element     # from both the arrays     x = max(a);     y = max(b);     print(x, y); Â
# Driver code if __name__ == "__main__" : Â
    a = [ 3, 2, 2 ];    n = len(a);          b = [ 1, 5, 7, 7, 9 ];     m = len(b); Â
    findNum(a, n, b, m);Â
# This code is contributed by AnkitRai01 |
C#
// C# implementation of the approachusing System;Â
class GFG{         // find maximum element in an array    static int max_element(int []a, int n)    {        int m = int.MinValue;                 for(int i = 0; i < n; i++)            m = Math.Max(m, a[i]);                 return m;    }         // Function to find the numbers from    // the given arrays such that their    // sum is not present in any    // of the given array    static void findNum(int []a, int n,                         int []b, int m)    {        // Find the maximum element        // from both the arrays        int x = max_element(a, n);        int y = max_element(b, m);        Console.Write(x + " " + y);    }         // Driver code    public static void Main()    {        int []a = { 3, 2, 2 };        int n = a.Length;        int []b = { 1, 5, 7, 7, 9 };        int m = b.Length;             findNum(a, n, b, m);    }}Â
// This code is contributed by kanugargng |
Javascript
<script>Â
// Javascript implementation of the approachÂ
// Function to find the numbers from// the given arrays such that their// sum is not present in any// of the given arrayfunction findNum(a, n, b, m){    // Find the maximum element    // from both the arrays    var x = a.reduce(function(a, b) { return Math.max(a, b); });    var y = b.reduce(function(a, b) { return Math.max(a, b); });    document.write(x + " " + y);}Â
// Driver codevar a = [ 3, 2, 2 ];var n = a.length;var b = [ 1, 5, 7, 7, 9 ]var m = b.length;findNum(a, n, b, m);Â
// This code is contributed by rutvik_56.</script> |
Output:Â
3 9
Â
Time Complexity: O(n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



