Open In App

Generate original Array from the bitwise AND and Bitwise OR of adjacent elements

Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer N denoting the size of an array and two arrays containing Bitwise AND and Bitwise OR of adjacent elements of the array and the first element of the array X, the task is to build the original array.

Examples:

Input: N = 2, X(First element) = 2
Bitwise OR = {3}, Bitwise AND = {2}
Output: {2, 3}

Input: N = 3, X(First element) = 3
Bitwise OR = {4, 3}, Bitwise AND = {3, 4}
Output: {3, 4, 3}

 

Approach: To solve the problem follow the below idea:

The problem can be solved using this mathematical relation -> A|B = A + B – A&B

Follow the given steps to solve the problem:

  • Iterate from i = 1 to N-1 to calculate the remaining array elements.
    • Use the formula stated above to generate array values
  • Then print all elements

Below is the implementation for the above approach:

C++




// C++ code for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function which will calculate the array elements
vector<int> solve(int N, int X, int OR[], int AND[])
{
    vector<int> a(N);
    a[0] = X;
 
    // Loop to calculate the array elements
    for (int i = 1; i < N; i++) {
        a[i] = OR[i - 1] + AND[i - 1] - a[i - 1];
    }
 
    // Return the original array
    return a;
}
 
// Driver code
int main()
{
    int N = 2, X = 2;
    int OR[] = { 3 };
    int AND[] = { 2 };
 
    // Function call
    vector<int> ans = solve(N, X, OR, AND);
    for (int i : ans)
        cout << i << " ";
 
    return 0;
}


Java




// Java code for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function which will calculate the array elements
    static int[] solve(int N, int X, int[] OR, int[] AND)
    {
        int[] a = new int[N];
        a[0] = X;
 
        // Loop to calculate the array elements
        for (int i = 1; i < N; i++) {
            a[i] = OR[i - 1] + AND[i - 1] - a[i - 1];
        }
 
        // return the original array
        return a;
    }
 
    public static void main(String[] args)
    {
        int N = 2, X = 2;
        int[] OR = { 3 };
        int[] AND = { 2 };
 
        // Function call
        int[] ans = solve(N, X, OR, AND);
        for (int i = 0; i < ans.length; i++) {
            System.out.print(ans[i] + " ");
        }
    }
}
 
// This code is contributed by lokesh (lokeshmvs21).


Python3




# python3 code for the above approach
 
# Function which will calculate the array elements
def solve(N, X, OR, AND) :
     
    a = [None] * N
     
    a[0] = X
 
    # Loop to calculate the array elements
    for i in range(1,N) :
        a[i] = OR[i - 1] + AND[i - 1] - a[i - 1]
 
    # Return the original array
    return a
     
   
# Driver code
if __name__ == "__main__" :
     
    N, X = 2,2
    OR = [ 3 ]
    AND = [ 2 ]
     
    # Function call
    ans = solve(N, X, OR, AND)
     
    for i in ans :
        print(i,end=' ')
 
# This code is contributed by adityapatil12


C#




// C# program for above approach:
using System;
class GFG {
 
  // Function which will calculate the array elements
  static int[] solve(int N, int X, int[] OR, int[] AND)
  {
    int[] a = new int[N];
    a[0] = X;
 
    // Loop to calculate the array elements
    for (int i = 1; i < N; i++) {
      a[i] = OR[i - 1] + AND[i - 1] - a[i - 1];
    }
 
    // return the original array
    return a;
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 2, X = 2;
    int[] OR = { 3 };
    int[] AND = { 2 };
 
    // Function call
    int[] ans = solve(N, X, OR, AND);
    for (int i = 0; i < ans.Length; i++) {
      Console.Write(ans[i] + " ");
    }
  }
}
 
// This code is contributed by code_hunt.


Javascript




<script>
    // JavaScript code for the above approach
 
 
    // Function which will calculate the array elements
    const solve = (N, X, OR, AND) => {
        let a = new Array(N).fill(0);
        a[0] = X;
 
        // Loop to calculate the array elements
        for (let i = 1; i < N; i++) {
            a[i] = OR[i - 1] + AND[i - 1] - a[i - 1];
        }
 
        // Return the original array
        return a;
    }
 
    // Driver code
 
    let N = 2, X = 2;
    let OR = [3];
    let AND = [2];
 
    // Function call
    let ans = solve(N, X, OR, AND);
    for (let i in ans)
        document.write(`${ans[i]} `);
 
// This code is contributed by rakeshsahni
 
</script>


Output

2 3 

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



Last Updated : 03 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads