The Wayback Machine - https://web.archive.org/web/20241003110057/https://www.geeksforgeeks.org/replace-every-array-element-by-bitwise-xor-of-previous-and-next-element/
Open In App

Replace every array element by Bitwise Xor of previous and next element

Last Updated : 06 Apr, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given an array of integers, replace every element with xor of previous and next elements with following exceptions. 

  1. First element is replaced by xor of first and second. 
  2. Last element is replaced by xor of last and second last.

Examples: 

Input: arr[] = { 2, 3, 4, 5, 6}
Output: 1 6 6 2 3 
We get the following array as {2^3, 2^4, 3^5, 4^6, 5^6}

Input: arr[] = { 1, 2, 1, 5}
Output: 3, 0, 7, 4
We get the following array as {1^2, 1^1, 2^5, 1^5}

A Simple Solution is to create an auxiliary array, copy contents of given array to auxiliary array. Finally traverse the auxiliary array and update given array using copied values. Time complexity of this solution is O(n), but it requires O(n) extra space.

C++




// C++ program to update every array element with
// sum of previous and next numbers in array
#include <iostream>
using namespace std;
 
void ReplaceElements(int arr[], int n)
{
    int newarr[n];//created a new array to store the elements.
    for(int i=0;i<n;i++){
        newarr[i]=arr[i];}
    arr[0]=arr[0]^arr[1];// changed the first element
    for(int i=1;i<n-1;i++){// iterated from second element to last second
        arr[i]=newarr[i-1]^newarr[i+1];}// element and changed every element according to question.
    arr[n-1]=newarr[n-1]^newarr[n-2];// changed the last element of the array.
}
 
// Driver program
int main()
{
    int arr[] = { 2, 3, 4, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    ReplaceElements(arr, n);
 
    // Print the modified array
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    return 0;
}
//This code is contributed by Naveen Gujjar from Haryana


Java




public class Main {
    public static void replaceElements(int[] arr, int n) {
        int[] newarr = new int[n]; // created a new array to store the elements.
        for (int i = 0; i < n; i++) {
            newarr[i] = arr[i];
        }
        arr[0] = arr[0] ^ arr[1]; // changed the first element
        for (int i = 1; i < n - 1; i++) { // iterated from second element to last second
            arr[i] = newarr[i - 1] ^ newarr[i + 1]; // element and changed every element according to the question.
        }
        arr[n - 1] = newarr[n - 1] ^ newarr[n - 2]; // changed the last element of the array.
    }
 
    // Driver program
    public static void main(String[] args) {
        int[] arr = { 2, 3, 4, 5, 6 };
        int n = arr.length;
 
        replaceElements(arr, n);
 
        // Print the modified array
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}


Python3




# Function to update every array element with sum of previous and next numbers in array
def replaceElements(arr, n):
    newarr = [0] * n  # created a new array to store the elements
    for i in range(n):
        newarr[i] = arr[i]
    arr[0] = arr[0] ^ arr[1]  # changed the first element
    for i in range(1, n-1):  # iterated from second element to last second element and changed every element according to question
        arr[i] = newarr[i-1] ^ newarr[i+1]
    # changed the last element of the array
    arr[n-1] = newarr[n-1] ^ newarr[n-2]
 
 
# Driver program
arr = [2, 3, 4, 5, 6]
n = len(arr)
replaceElements(arr, n)
 
# Print the modified array
for i in range(n):
    print(arr[i], end=" ")


Javascript




// JS program to update every array element with
// sum of previous and next numbers in array
 
function ReplaceElements(arr, n) {
  let newarr = [...arr]; // created a new array to store the elements.
  arr[0] = arr[0] ^ arr[1]; // changed the first element
  for (let i = 1; i < n - 1; i++) {
    // iterated from second element to last second element
    arr[i] = newarr[i - 1] ^ newarr[i + 1]; // element and changed every element according to question.
  }
  arr[n - 1] = newarr[n - 1] ^ newarr[n - 2]; // changed the last element of the array.
}
 
// Driver program
let arr = [2, 3, 4, 5, 6];
let n = arr.length;
 
ReplaceElements(arr, n);
 
// Print the modified array
for (let i = 0; i < n; i++) {
  console.log(arr[i] + " ");
}


C#




// C# program to update every array element with
// sum of previous and next numbers in array
using System;
 
class MainClass {
    static void ReplaceElements(int[] arr, int n)
    {
        int[] newarr = new int[n]; // created a new array to
                                   // store the elements.
        for (int i = 0; i < n; i++) {
            newarr[i] = arr[i];
        }
        arr[0]
            = arr[0] ^ arr[1]; // changed the first element
        for (int i = 1; i < n - 1;
             i++) { // iterated from second element to last
                    // second
            arr[i]
                = newarr[i - 1]
                  ^ newarr[i + 1]; // element and changed
                                   // every element according
                                   // to question.
        }
        arr[n - 1]
            = newarr[n - 1]
              ^ newarr[n - 2]; // changed the last element
                               // of the array.
    }
 
    public static void Main()
    {
        int[] arr = { 2, 3, 4, 5, 6 };
        int n = arr.Length;
 
        ReplaceElements(arr, n);
 
        // Print the modified array
        for (int i = 0; i < n; i++)
            Console.Write(arr[i] + " ");
    }
}


Output

1 6 6 2 3 

Time Complexity: O(n)
Auxiliary Space: O(n)

An efficient solution can solve the problem in O(n) time and O(1) space. The idea is to keep track of previous element in loop. Xor the previous element using the extra variable and the next element to get each element.

Below is the implementation of the above approach:  

C++




// C++ program to update every array element with
// sum of previous and next numbers in array
#include <iostream>
using namespace std;
 
void ReplaceElements(int arr[], int n)
{
    // Nothing to do when array size is 1
    if (n <= 1)
        return;
 
    // store current value of arr[0] and update it
    int prev = arr[0];
    arr[0] = arr[0] ^ arr[1];
 
    // Update rest of the array elements
    for (int i = 1; i < n - 1; i++) {
        // Store current value of next interaction
        int curr = arr[i];
 
        // Update current value using previous value
        arr[i] = prev ^ arr[i + 1];
 
        // Update previous value
        prev = curr;
    }
 
    // Update last array element separately
    arr[n - 1] = prev ^ arr[n - 1];
}
 
// Driver program
int main()
{
    int arr[] = { 2, 3, 4, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    ReplaceElements(arr, n);
 
    // Print the modified array
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    return 0;
}


Java




// Java  program to update every array
// element with sum of previous and
// next numbers in array
import  java .io.*;
 
class GFG
{
static void ReplaceElements(int[] arr,
                            int n)
{
    // Nothing to do when array size is 1
    if (n <= 1)
        return;
 
    // store current value of arr[0]
    // and update it
    int prev = arr[0];
    arr[0] = arr[0] ^ arr[1];
 
    // Update rest of the array elements
    for (int i = 1; i < n - 1; i++)
    {
        // Store current value of
        // next interaction
        int curr = arr[i];
 
        // Update current value using
        // previous value
        arr[i] = prev ^ arr[i + 1];
 
        // Update previous value
        prev = curr;
    }
 
    // Update last array element separately
    arr[n - 1] = prev ^ arr[n - 1];
}
 
// Driver Code
public static void main(String[] args)
 
{
    int[] arr = { 2, 3, 4, 5, 6 };
    int n = arr.length;
 
    ReplaceElements(arr, n);
 
    // Print the modified array
    for (int i = 0; i < n; i++)
        System.out.print(arr[i] + " ");
}
}
 
// This code is contributed
// by anuj_67..


Python3




# Python3 program to update every
# array element with sum of previous
# and next numbers in array
def ReplaceElements(arr, n):
 
    # Nothing to do when array
    # size is 1
    if n <= 1:
        return
 
    # store current value of arr[0]
    # and update it
    prev = arr[0]
    arr[0] = arr[0] ^ arr[1]
 
    # Update rest of the array elements
    for i in range(1, n - 1):
 
        # Store current value of
        # next interaction
        curr = arr[i]
         
        # Update current value using
        # previous value
        arr[i] = prev ^ arr[i + 1]
         
        # Update previous value
        prev = curr
     
    # Update last array element separately
    arr[n - 1] = prev ^ arr[n - 1]
 
# Driver Code
arr = [2, 3, 4, 5, 6]
n = len(arr)
ReplaceElements(arr, n)
for i in range(n):
    print(arr[i], end = " ")
 
# This code is contributed
# by Shrikant13


C#




// C# program to update every array
// element with sum of previous and
// next numbers in array
using System;
 
class GFG
{
static void ReplaceElements(int[] arr,
                            int n)
{
    // Nothing to do when array size is 1
    if (n <= 1)
        return;
 
    // store current value of arr[0]
    // and update it
    int prev = arr[0];
    arr[0] = arr[0] ^ arr[1];
 
    // Update rest of the array elements
    for (int i = 1; i < n - 1; i++)
    {
        // Store current value of
        // next interaction
        int curr = arr[i];
 
        // Update current value using
        // previous value
        arr[i] = prev ^ arr[i + 1];
 
        // Update previous value
        prev = curr;
    }
 
    // Update last array element separately
    arr[n - 1] = prev ^ arr[n - 1];
}
 
// Driver Code
public static void Main()
{
    int[] arr = { 2, 3, 4, 5, 6 };
    int n = arr.Length;
 
    ReplaceElements(arr, n);
 
    // Print the modified array
    for (int i = 0; i < n; i++)
        Console.Write(arr[i] + " ");
}
}
 
// This code is contributed
// by Akanskha Rai(Abby_akku)


PHP




<?php
// PHP program to update every array
// element with sum of previous and
// next numbers in array
function ReplaceElements(&$arr, $n)
{
    // Nothing to do when array size is 1
    if ($n <= 1)
        return;
 
    // store current value of arr[0]
    // and update it
    $prev = $arr[0];
    $arr[0] = $arr[0] ^ $arr[1];
 
    // Update rest of the array elements
    for ($i = 1; $i < $n - 1; $i++)
    {
        // Store current value of next
        // interaction
        $curr = $arr[$i];
 
        // Update current value using
        // previous value
        $arr[$i] = $prev ^ $arr[$i + 1];
 
        // Update previous value
        $prev = $curr;
    }
 
    // Update last array element separately
    $arr[$n - 1] = $prev ^ $arr[$n - 1];
}
 
// Driver Code
$arr = array( 2, 3, 4, 5, 6 );
$n = sizeof($arr);
 
ReplaceElements($arr, $n);
 
// Print the modified array
for ($i = 0; $i < $n; $i++)
    echo $arr[$i] . " ";
 
// This code is contributed by ita_c
?>


Javascript




<script>
 
// Javascript program to update every array
// element with sum of previous and next
// numbers in array
function ReplaceElements(arr, n)
{
     
    // Nothing to do when array size is 1
    if (n <= 1)
        return;
 
    // Store current value of arr[0] and update it
    let prev = arr[0];
    arr[0] = arr[0] ^ arr[1];
 
    // Update rest of the array elements
    for(let i = 1; i < n - 1; i++)
    {
         
        // Store current value of next interaction
        let curr = arr[i];
 
        // Update current value using previous value
        arr[i] = prev ^ arr[i + 1];
 
        // Update previous value
        prev = curr;
    }
 
    // Update last array element separately
    arr[n - 1] = prev ^ arr[n - 1];
}
 
// Driver code
let arr = [ 2, 3, 4, 5, 6 ];
let n = arr.length;
 
ReplaceElements(arr, n);
 
// Print the modified array
for(let i = 0; i < n; i++)
    document.write(arr[i] + " ");
     
// This code is contributed by subhammahato348
 
</script>


Output

1 6 6 2 3 

Time complexity: O(N), where N is the number of elements in the given array.
Auxiliary space: O(1)



Similar Reads

Replace every array element by sum of previous and next
Given an array of integers, update every element with sum of previous and next elements with following exceptions. First element is replaced by sum of first and second. Last element is replaced by sum of last and second last. Examples: Input : arr[] = { 2, 3, 4, 5, 6} Output : 5 6 8 10 11 Explanation: We get the following array as {2+3, 2+4, 3+5, 4
7 min read
Replace every array element with maximum of K next and K previous elements
Given an array arr, the task is to replace each array element by the maximum of K next and K previous elements. Example: Input: arr[] = {12, 5, 3, 9, 21, 36, 17}, K=2Output: 5 12 21 36 36 21 36 Input: arr[] = { 13, 21, 19}, K=1Output: 21, 19, 21 Naive Approach: Follow the below steps to solve this problem: Traverse the array from i=0 to i<N and
15 min read
Replace every array element by multiplication of previous and next
Given an array of integers, update every element with the multiplication of previous and next elements with the following exceptions. a) The First element is replaced by the multiplication of the first and second. b) The last element is replaced by multiplication of the last and second last. Example: Input : arr[] = {2, 3, 4, 5, 6} Output : arr[] =
7 min read
Replace every element of the array with BitWise XOR of all other
Given an array of integers. The task is to replace every element by the bitwise xor of all other elements of the array. Examples: Input: arr[] = { 2, 3, 3, 5, 5 } Output: 0 1 1 7 7 Bitwise Xor of 3, 3, 5, 5 = 0 Bitwise Xor of 2, 3, 5, 5 = 1 Bitwise Xor of 2, 3, 5, 5 = 1 Bitwise Xor of 2, 3, 3, 5 = 7 Bitwise Xor of 2, 3, 3, 5 = 7 Input : arr[] = { 1
5 min read
Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
Given an array arr[] of size N. The task is to find the number of pairs (arr[i], arr[j]) as cntAND, cntOR, and cntXOR such that: cntAND: Count of pairs where bitwise AND of least significant bits is 1.cntOR: Count of pairs where bitwise OR of least significant bits is 1.cntXOR: Count of pairs where bitwise XOR of least significant bits is 1. Exampl
7 min read
Generate an array having Bitwise AND of the previous and the next element
Given an array of integers arr[] of N elements, the task is to generate another array having (Bitwise) AND of previous and next elements with the following exceptions. The first element is the bitwise AND of the first and the second element.The last element is the bitwise AND of the last and the second last element. Examples: Input: arr[] = {1, 2,
5 min read
Maximize count of pairs whose Bitwise AND exceeds Bitwise XOR by replacing such pairs with their Bitwise AND
Given an array arr[] consisting of N positive integers, replace pairs of array elements whose Bitwise AND exceeds Bitwise XOR values by their Bitwise AND value. Finally, count the maximum number of such pairs that can be generated from the array. Examples: Input: arr[] = {12, 9, 15, 7}Output: 2Explanation:Step 1: Select the pair {12, 15} and replac
9 min read
Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values
Given two integers X and Y, representing Bitwise XOR and Bitwise AND of two positive integers, the task is to calculate the Bitwise OR value of those two positive integers. Examples: Input: X = 5, Y = 2 Output: 7 Explanation: If A and B are two positive integers such that A ^ B = 5, A & B = 2, then the possible value of A and B is 3 and 6 respe
7 min read
Maximize count of pairs whose bitwise XOR is even by replacing such pairs with their Bitwise XOR
Given an array arr[] of size N, the task is to replace a pair of array elements whose Bitwise XOR is even by their Bitwise XOR. Repeat the above step as long as possible. Finally, print the count of such operations performed on the array Examples: Input: arr[] = { 4, 6, 1, 3 }Output: 3Explanation:Step 1: Remove the pair (4, 6) and replace them by t
11 min read
Replace all elements of given Array with average of previous K and next K elements
Given an array arr[] containing N positive integers and an integer K. The task is to replace every array element with the average of previous K and next K elements. Also, if K elements are not present then adjust use the maximum number of elements available before and after. Examples: Input: arr[] = {9, 7, 3, 9, 1, 8, 11}, K=2Output: 5 7 6 4 7 7 4E
11 min read
Replace every element in a circular array by sum of next K elements
Given a circular array arr[] of N integers and an integer K, the task is to print the array after the following operations: If K is non-negative, then replace A[i] with the sum of the next K elements.If K is negative, then replace it with the sum of previous K elements. A cyclic array means the next element of the last element of the array is the f
15+ min read
Queries to replace every array element by its XOR with a given value with updates
Given an array, initially consisting of 0 as the only element present and Q operations of the following two types: Add(X): Insert X into the array.Update(X): Replace every array element Ai by Ai ^ X, where ^ is the XOR operation. Examples: Input: Q = 2Add(5)Update(2)Output: 2 7Explanation:Initially, Array = {0}Query 1: Array = {0, 5}Query 2: Array
5 min read
Maximum element in an array such that its previous and next element product is maximum
Given an array arr[] of N integers, the task is to print the largest element among the array such that its previous and next element product is maximum.Examples: Input: arr[] = {5, 6, 4, 3, 2} Output: 6 The product of the next and the previous elements for every element of the given array are: 5 -> 2 * 6 = 12 6 -> 5 * 4 = 20 4 -> 6 * 3 = 1
6 min read
Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
Given an array of N(always a power of 2) elements and Q queries.Every query consists of two elements, an index, and a value... We need to write a program that assigns value to Aindex and prints the single element which is left after performing the below operations for each query: At alternate steps, perform bitwise OR and bitwise XOR operations on
15+ min read
Bitwise XOR of same indexed array elements after rearranging an array to make XOR of same indexed elements of two arrays equal
Given two arrays A[] and B[] consisting of N positive integers, the task is to the Bitwise XOR of same indexed array elements after rearranging the array B[] such that the Bitwise XOR of the same indexed elements of the arrays A[] becomes equal. Examples: Input: A[] = {1, 2, 3}, B[] = {4, 6, 7}Output: 5Explanation:Below are the possible arrangement
14 min read
Range Query on array whose each element is XOR of index value and previous element
Consider an arr[] which can be defined as: You are given Q queries of the form [l, r]. The task is to output the value of arr[l] ? arr[l+1] ? ..... ? arr[r-1] ? arr[r] for each query. Examples : Input : q = 3 q1 = { 2, 4 } q2 = { 2, 8 } q3 = { 5, 9 } Output : 7 9 15 The beginning of the array with constraint look like: arr[] = { 0, 1, 3, 0, 4, 1, 7
9 min read
Count pairs with bitwise XOR exceeding bitwise AND from a given array
Given an array, arr[] of size N, the task is to count the number of pairs from the given array such that the bitwise AND(&) of each pair is less than its bitwise XOR(^). Examples: Input: arr[] = {1, 2, 3, 4, 5} Output: 8Explanation: Pairs that satisfy the given conditions are: (1 & 2) < (1 ^ 2)(1 & 3) < (1 ^ 3)(1 & 4) < (1
10 min read
Check if any Array pair has bitwise XOR greater than bitwise AND
Given an array arr[] of size N, the task is to find if there exists a pair in the array, such that their bitwise XOR is greater than their bitwise AND i.e. arr[i] ⊕ arr[j] > arr[i] & arr[j], (0 ≤ i < j ≤ N-1) where ⊕ represents the Bitwise XOR operator and & represents bitwise AND operator. Examples: Input: arr[] = {4, 5, 8, 6}Output:
9 min read
Generate Array whose average and bitwise OR of bitwise XOR are equal
Given an integer N (N is odd). the task is to construct an array arr[] of size N where 1 ? arr[i] ? N such that the bitwise OR of the bitwise XOR of every consecutive pair should be equal to the average of the constructed arr[]. Formally: (arr[0] ^ arr[1]) | (arr[2] ^ arr[3] ) | (arr[4] ^ arr[5]) . . . (arr[N-3] ^ arr[N-2] ) | arr[N-1] = ( arr[0] +
4 min read
Replace every node of a Linked list with the next greater element on right side
Given a linked list, the task is to find the Next Greater Element for every node of the linked list. Note: For nodes with no next greater element, store -1 in the result. Examples: Input: linked list = [2, 1, 5] Output: [5, 5, -1] Input: linked list = [2, 7, 4, 3, 5] Output: [7, -1, 5, 5, -1] Approach: To solve the problem mentioned above the main
6 min read
Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
Given two integers X and Y, the task is to find the total number of ways to generate a pair of integers A and B such that Bitwise XOR and Bitwise AND between A and B is X and Y respectively Examples: Input: X = 2, Y = 5Output: 2Explanation:The two possible pairs are (5, 7) and (7, 5).Pair 1: (5, 7)Bitwise AND = 5 & 7 = 2Bitwise XOR = 5 ^ 7 = 5P
7 min read
Count array elements exceeding all previous elements as well as the next array element
Given an array arr[], the task is to find the count of array elements satisfying the following conditions: The array elements should be strictly greater than all the previously occurring array elements.Either it is the last array element or the integer should be strictly larger than the next array element. Note: The first integer of the array can a
6 min read
Split an array into subarrays with maximum Bitwise XOR of their respective Bitwise OR values
Given an array arr[] consisting of N integers, the task is to find the maximum Bitwise XOR of Bitwise OR of every subarray after splitting the array into subarrays(possible zero subarrays). Examples: Input: arr[] = {1, 5, 7}, N = 3Output: 7Explanation:The given array can be expressed as the 1 subarray i.e., {1, 5, 7}.The Bitwise XOR of the Bitwise
9 min read
Find the triplet from given Bitwise XOR and Bitwise AND values of all its pairs
Given six positive integers representing the Bitwise XOR and Bitwise AND of all possible pairs of a triplet (a, b, c), the task is to find the triplet. Examples: Input: aXORb = 30, aANDb = 0, aXORc = 10, aANDc = 20, aXORb = 20, aANDb = 10 Output: a = 10, b = 20, c= 30 Explanation: If a = 10, b = 20, c= 30 a ^ b = 30, a & b = 0 a ^ c = 10, a
7 min read
Count Number of Pairs where Bitwise AND and Bitwise XOR is Equal
Given an integer array arr of size N, the task is to count the number of pairs whose BITWISE AND and BITWISE XOR are equal. Example: Input: N = 3, arr[] = {0,0,1}Output: 1Explanation: There is only one pair arr[0] and arr[1] as 0&0=0 and 0^0=0 Input: N = 4, arr[] = {1, 2, 4, 8}Output: 0Explanation: There are no pairs satisfying the condition. A
4 min read
Replace all array elements with the nearest power of its previous element
Given a circular array arr[] consisting of N integers, the task is to replace all the array elements with the nearest possible power of its previous adjacent element Examples: Input: arr[] = {2, 4, 6, 3, 11}Output: 1 4 4 6 9 Explanation:Power of 11 which is nearest to 2 ---> 110 = 1Power of 2 which is nearest to 4 ---> 22 = 4Power of 4 which
6 min read
Elements greater than the previous and next element in an Array
Given an array of N integers. The task is to print the elements from the array which are greater than their immediate previous and next elements.Examples : Input : arr[] = {2, 3, 1, 5, 4, 9, 8, 7, 5} Output : 3, 5, 9 In above given example 3 is greater than its left element 2 and right element 1. Similar logic is applied to other elements hence our
5 min read
Count pairs with odd Bitwise XOR that can be removed and replaced by their Bitwise OR
Given an array arr[] consisting of N integers, the task is to count the number of pairs whose Bitwise XOR is odd, that can be removed and replaced by their Bitwise OR values until no such pair exists in the array. Examples: Input: arr[] = {5, 4, 7, 2}Output: 2Explanation:Pair (5, 4): Bitwise XOR of 5 and 4 is 1. Remove this pair and add their Bitwi
5 min read
Bitwise XOR of Bitwise AND of all pairs from two given arrays
Given two arrays arr1[] and arr2[] consisting of N and M integers respectively, the task is to print the Bitwise XOR of Bitwise AND of all pairs possible by selecting an element from arr1[] and arr2[]. Examples: Input: arr1[] = {1, 2, 3}, arr2[] = {6, 5}Output: 0Explanation: Bitwise AND of the pair (arr1[0], arr2[]) = 1 & 6 = 0.Bitwise AND of t
10 min read
Find size of largest subset with bitwise AND greater than their bitwise XOR
Given an array arr[] of N integers, the task is to find the size of the largest subset such that the bitwise AND of all elements of the subset is greater than the bitwise XOR of all elements of the subset. Example: Input: arr[] = {1, 2, 3, 4, 5}Output: 2Explanation: The subset {2, 3} has the bitwise AND of all elements as 2 while the bitwise XOR of
6 min read