Skip to main content

Add an element in Array to make the bitwise XOR as K

Given an array arr[] containing N positive integers, the task is to add an integer such that the bitwise Xor of the new array becomes K.

Examples:

Input: arr[] = 1, 4, 5, 6, K = 4
Output: 2
Explanation: Bit-wise XOR of the array is 6. 
And bit-wise XOR of 6 and 2 is 4.

Input: arr[] = 2, 7, 9, 1, K = 5
Output: 8

 

Approach: The solution to the problem is based on the following idea of bitwise Xor:

If for two numbers X and Y, the bitwise Xor of X and Y is Z then the bitwise Xor of X and Z is Y.

Follow the steps to solve the problem:

  • Let the bitwise XOR of the array elements be X
  • Say the required value to be added is Y such that X Xor Y = K.
  • From the above observation, it is clear that the value to be added (Y) is the same as X Xor K.

Below is the implementation of the above approach:

C++




// C++ code to implement the above approach
 
#include
using namespace std;
 
// Function to find the required value
int find_K(int K, vector<int>& arr)
    int XOR = 0;
 
    // Find XOR of all the array elements
    for (int i = 0; i < arr.size(); i++)
        XOR ^= arr[i];
 
    // K = XOR^N, where N is size of array
    return XOR ^ K;
 
// Drivers code
int main()
    int K = 4;
    vector<int> arr = 1, 4, 5, 6 ;
 
    // Function call
    cout << find_K(K, arr);
    return 0;

Java




// Java code to implement the above approach
import java.io.*;
 
class GFG
    // Function to find the required value
    public static int find_K(int K, int arr[])
    
        int XOR = 0;
 
        // Find XOR of all the array elements
        for (int i = 0; i < arr.length; i++)
            XOR ^= arr[i];
 
        // K = XOR^N, where N is size of array
        return XOR ^ K;
    
 
    // Driver Code
    public static void main(String[] args)
    
        int K = 4;
        int arr[] = 1, 4, 5, 6 ;
 
        // Function call
        System.out.print(find_K(K, arr));
    
 
// This code is contributed by Rohit Pradhan

Python3




# Python code to implement the above approach
 
# Function to find the required value
def find_K(K, arr):
    XOR = 0
 
    # Find XOR of all the array elements
    for i in range(len(arr)):
        XOR ^= arr[i]
 
    # K = XOR^N, where N is size of array
    return XOR ^ K
 
# Drivers code
K = 4
arr = [ 1, 4, 5, 6 ]
 
# Function call
print(find_K(K, arr))
 
# This code is contributed by shinjanpatra

C#




// C# code to implement the above approach
using System;
 
class GFG
 
  // Function to find the required value
  static int find_K(int K, int[] arr)
  
    int XOR = 0;
 
    // Find XOR of all the array elements
    for (int i = 0; i < arr.Length; i++)
      XOR ^= arr[i];
 
    // K = XOR^N, where N is size of array
    return XOR ^ K;
  
 
  // Driver Code
  public static int Main()
  
    int K = 4;
    int[] arr = new int[] 1, 4, 5, 6 ;
 
    // Function call
    Console.Write(find_K(K, arr));
    return 0;
  
 
// This code is contributed by Taranpreet

Javascript





Output
2

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

https://neveropen.tech/add-an-element-in-array-to-make-the-bitwise-xor-as-k/?feed_id=175&_unique_id=683d4d91b3aad

Comments

Popular posts from this blog

Bare Metal Billing Client Portal Guide

Contents Order a Bare Metal Server My Custom / Contract Pricing View Contract Details Location Management Order History & Status View Order Details Introduction The phoenixNAP Client Portal allows you to purchase bare metal servers and other phoenixNAP products and services. Using the intuitive interface and its essential tools, you can also easily manage your infrastructure. This quick guide will show you how to use the new form to order a bare metal server and how to navigate through new bare metal features within the phoenixNAP Client Portal. Order a Bare Metal Server An order form is an accordion-based process for purchasing phoenixNAP products. Our order form allows you to view the pricing and order multiple products from the same category at the same time. Note: The prices on the form are per month . A contract is not required. However, if you want a contracted price, you may be eligible for a discount depending on the quantity and ...

Find pair of integers such that their sum is twice their Bitwise XOR

Given a positive integer N, the task is to find all pairs of integers (i, j) from the range [1, N] in increasing order of i such that: 1 ? i, j ? N i + j = N i + j = 2*(i ^ j) If there are no such pairs, return a pair -1, -1. Note: Here ‘^’ denotes the bitwise XOR operation. Examples: Input: N = 4 Output: 1, 3, 3, 1 Explanation: A total of 3 pairs satisfy the first condition: (1, 3), (2, 2), (3, 1). There are only two valid pairs out of them: (1, 3) and (3, 1) as 1 + 3 = 4 = 2 * (1 ^ 3). Input: 7 Output: -1, -1 Input: 144 Output: 36, 108,  44, 100, 100, 44,  108, 36 Approach:   The problem can be viewed as a bitwise manipulation problem satisfying pre-conditions.  If the pairs add upto N then it is obvious that the second element j of the pair can be generated using the first element i as j = N – i . Then we just have to check for the remaining condition   i + j = 2 * (i ^ j). Follow the steps mentioned below to solve the problem: Traverse from ...