Append two elements to make the array satisfy the given condition

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(S2) 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Output:

6 14

Time Complexity: O(n)



My Personal Notes arrow_drop_up

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