# 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 og the arrays.

Below is the implementation of the above approach:

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find the numbers from ` `// the given arrays such that their ` `// sum is not present in any ` `// of the given array ` `void` `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 code ` `int` `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 implementation of the approach ` `class` `GFG ` `{ ` `     `  `// find maximum element in an array ` `static` `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 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); ` `    ``System.out.print(x + ``" "` `+ y); ` `} ` ` `  `// Driver code ` `public` `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 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# implementation of the approach ` `using` `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 `

Output:
```3 9
```

Time Complexity: O(1)

Check out this Author's contributed articles.

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 : AnkitRai01, andrew1234, kanugargng

Article Tags :
Practice Tags :