Given an array **arr[]** of non-negative integers, let’s define **X** as the XOR of all the array elements and **S** as the sum of all the array elements. The task is to find two elements such that when they are appended to the array **S = 2 * X** is satisfied for the updated array.

**Examples:**

Input:arr[] = {1, 7}

Output:6 14

Initially S = 8, and X = 6. After appending 6

and 14, S_NEW = (8 + 6 + 14) = 28

and X_NEW = (6 ^ 6 ^ 14) = 14

Clearly, S_NEW = 2 * X_NEW

Input:arr[] = {1, 3}

Output:2 6

**Naive approach:** Run two nested loops from **1** to **S** and check for each pair whether it satisfies the condition or not. This will take **O(S ^{2})** time.

**Efficient approach:** It can be observed that if **X** and **S + X** are appended to the array then **S_NEW = 2 * (S + X)** and **X_NEW = S + X** which satisfy the given condition.

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 required numbers ` `void` `findNums(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `// Find the sum and xor ` ` ` `int` `S = 0, X = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `S += arr[i]; ` ` ` `X ^= arr[i]; ` ` ` `} ` ` ` ` ` `// Print the required elements ` ` ` `cout << X << ` `" "` `<< (X + S); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 7 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` ` ` `findNums(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find the required numbers ` ` ` `static` `void` `findNums(` `int` `arr[], ` `int` `n) ` ` ` `{ ` ` ` ` ` `// Find the sum and xor ` ` ` `int` `S = ` `0` `, X = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `S += arr[i]; ` ` ` `X ^= arr[i]; ` ` ` `} ` ` ` ` ` `// Print the required elements ` ` ` `System.out.println(X + ` `" "` `+ (X + S)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `arr[] = { ` `1` `, ` `7` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `findNums(arr, n); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to find the required numbers ` `def` `findNums(arr, n) : ` ` ` ` ` `# Find the sum and xor ` ` ` `S ` `=` `0` `; X ` `=` `0` `; ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `S ` `+` `=` `arr[i]; ` ` ` `X ^` `=` `arr[i]; ` ` ` ` ` `# Print the required elements ` ` ` `print` `(X, X ` `+` `S); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[ ` `1` `, ` `7` `]; ` ` ` `n ` `=` `len` `(arr); ` ` ` ` ` `findNums(arr, n); ` ` ` `# This code is contributed by AnkiRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find the required numbers ` ` ` `static` `void` `findNums(` `int` `[]arr, ` `int` `n) ` ` ` `{ ` ` ` ` ` `// Find the sum and xor ` ` ` `int` `S = 0, X = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `S += arr[i]; ` ` ` `X ^= arr[i]; ` ` ` `} ` ` ` ` ` `// Print the required elements ` ` ` `Console.WriteLine(X + ` `" "` `+ (X + S)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[]arr = { 1, 7 }; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `findNums(arr, n); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

6 14

**Time Complexity:** O(n)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Choose X elements from A[] and Y elements from B[] which satisfy the given condition
- Sum of absolute differences of pairs from the given array that satisfy the given condition
- Pairs from an array that satisfy the given condition
- Count of indices in an array that satisfy the given condition
- Find the Number of Permutations that satisfy the given condition in an array
- Count of sub-sequences which satisfy the given condition
- Count sub-sets that satisfy the given condition
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition
- Count all possible N digit numbers that satisfy the given condition
- Count index pairs which satisfy the given condition
- Check if a cycle of length 3 exists or not in a graph that satisfy a given condition
- Number of cells in a matrix that satisfy the given condition
- Mimimum number of leaves required to be removed from a Tree to satisfy the given condition
- Remove elements to make array satisfy arr[ i+1] < arr[i] for each valid i
- Number of strings in two array satisfy the given conditions
- Generate an array of K elements such that sum of elements is N and the condition a[i] < a[i+1] <= 2*a[i] is met | Set 2
- Append a digit in the end to make the number equal to the length of the remaining string
- Check if elements of an array can be arranged satisfying the given condition
- Count of triplets in an array that satisfy the given conditions
- Count of unordered pairs (x, y) of Array which satisfy given equation

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.