Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Find pair i, j such that |A[i]−A[j]| is same as sum of difference of them with any Array element
Next article icon

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

Last Updated : 18 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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 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




// 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




# 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 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
 
 

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 array
function 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 code
var 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)



Next Article
Find pair i, j such that |A[i]−A[j]| is same as sum of difference of them with any Array element
author
spp____
Improve
Article Tags :
  • Arrays
  • DSA
  • Mathematical
Practice Tags :
  • Arrays
  • Mathematical

Similar Reads

  • Count of triplets from the given Array such that sum of any two elements is the third element
    Given an unsorted array arr, the task is to find the count of the distinct triplets in which the sum of any two elements is the third element. Examples: Input: arr[] = {1, 3, 4, 15, 19} Output: 2 Explanation: In the given array there are two triplets such that the sum of the two elements is equal to
    7 min read
  • Find three element from given three arrays such that their sum is X | Set 2
    Given three sorted integer arrays A[], B[] and C[], the task is to find three integers, one from each array such that they sum up to a given target value X. Print Yes or No depending on whether such triplet exists or not.Examples: Input: A[] = {2}, B[] = {1, 6, 7}, C[] = {4, 5}, X = 12 Output: Yes A
    9 min read
  • Minimize the sum after choosing elements from the given three arrays
    Given three arrays A[], B[] and C[] of same size N. The task is to minimize the sum after choosing N elements from these array such that at every index i an element from any one of the array A[i], B[i] or C[i] can be chosen and no two consecutive elements can be chosen from the same array.Examples:
    15+ min read
  • Find pair i, j such that |A[i]−A[j]| is same as sum of difference of them with any Array element
    Given an array A[] having N non-negative integers, find a pair of indices i and j such that the absolute difference between them is same as the sum of differences of those with any other array element i.e., | A[i] − A[k] | + | A[k]− A[j] | = | A[i] − A[j] |, where k can be any index. Examples: Input
    7 min read
  • Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
    Given an array of integers arr[], the task is to count the number of sub-arrays such that the average of elements present in the sub-array is greater than the average of elements that are not present in the sub-array.Examples: Input: arr[] = {6, 3, 5} Output: 3 The sub-arrays are {6}, {5} and {6, 3,
    8 min read
  • Find an element in array such that sum of left array is equal to sum of right array
    Given, an array of size n. Find an element that divides the array into two sub-arrays with equal sums. Examples: Input: 1 4 2 5 0Output: 2Explanation: If 2 is the partition, subarrays are : [1, 4] and [5] Input: 2 3 4 1 4 5Output: 1Explanation: If 1 is the partition, Subarrays are : [2, 3, 4] and [4
    15+ min read
  • Number of indices pair such that element pair sum from first Array is greater than second Array
    Given two integer arrays A[] and B[] of equal sizes, the task is to find the number of pairs of indices {i, j} in the arrays such that A[i] + A[j] > B[i] + B[j] and i < j.Examples: Input: A[] = {4, 8, 2, 6, 2}, B[] = {4, 5, 4, 1, 3} Output: 7 Explanation: There are a total of 7 pairs of indice
    15+ min read
  • Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
    Given two unsorted arrays arr1[] and arr2[], the task is to find the sum of elements of arr1[] such that the number of elements less than or equal to them in arr2[] is maximum. Examples: Input: arr1[] = {1, 2, 3, 4, 7, 9}, arr2[] = {0, 1, 2, 1, 1, 4} Output: 20 Below table shows the count of element
    15 min read
  • Find sub-arrays from given two arrays such that they have equal sum
    Given two arrays A[] and B[] of equal sizes i.e. N containing integers from 1 to N. The task is to find sub-arrays from the given arrays such that they have equal sum. Print the indices of such sub-arrays. If no such sub-arrays are possible then print -1.Examples: Input: A[] = {1, 2, 3, 4, 5}, B[] =
    15+ min read
  • Count of elements which is the sum of a subarray of the given Array
    Given an array arr[], the task is to count elements in an array such that there exists a subarray whose sum is equal to this element.Note: Length of subarray must be greater than 1. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: 4 Explanation: There are 4 such elements in array - arr[2] = 3
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences