You are given an array of integers A and many queries in the form of a 2-D integer array B.

0

You are given an array of integers A and many queries in the form of a 2-D integer array B. In each query, you are given 4 integers l1, r1, l2, r2. 

You are given an array of integers A and many queries in the form of a 2-D integer array B. In each query, you are given 4 integers l1, r1, l2, r2.
Software Engineering practical Coding Questions

These integers represent two ranges, one that goes from l1 to r1 and another goes from l2 to r2. You need to return the bitwise XOR of element-wise bitwise AND of integers in these two ranges. That is, first do bitwise AND of all integers in l1 to r1, let's call it x1, similarly do it for l2 to r2 and we get x2. Now, the answer for this query would be x1 XOR x2. Return an integer array with the ith index containing the answer for the ith query.

Problem Constraints

1 <= A.size() <= 1e5

1 <= A[i] <= 1e9

1 <= B.size() <= 1e5

1 <= B[i][0], B[i][1], B[i][2], B[i][3] <= A.size()

B[i][0] <= B[i][1]

B[i][2] <= B[i][3]

Input Format

First argument is the integer array A.

Second argument is the 2-D integer array B.

Output Format

Return a single integer array as per the given problem.

Example Input

Input 1:

A = [3, 1, 7]

B = [ [1, 1, 3, 3] ]

Input 2:

A = [8, 6, 5, 9, 7, 7, 9, 3, 8]

B = [ [3, 5, 5, 5] ]

Example Output

Output 1:

[4]

Output 2:

[6]

Example Explanation

Explanation 1:

Here, result of element-wise AND of first range is 3 and for second range, it is is 7. Their bitwise XOR is 4.

Explanation 2:

Here, result of element-wise AND of first range is 1 and for second range, it is is 7. Their bitwise XOR is 6.


 Sol :-


To solve this problem, we can iterate through the queries and compute the bitwise AND and XOR of the elements in each range.

Here is some sample code in Python that demonstrates this approach:


Python
def bitwise_and_xor(A, B):
# Initialize the result array
result = []
for l1, r1, l2, r2 in B:
# Initialize the bitwise AND and XOR to the first element in the first range
and_result = A[l1-1]
xor_result = A[l1-1]
# Iterate through the elements in the first range and compute the bitwise AND and XOR
for i in range(l1, r1+1):
and_result &= A[i-1]
xor_result ^= A[i-1]
# Iterate through the elements in the second range and compute the bitwise AND and XOR
for i in range(l2, r2+1):
and_result &= A[i-1]
xor_result ^= A[i-1]
# Append the bitwise AND XOR to the result array
result.append(and_result ^ xor_result)
# Return the result array
return result

# Test the function
print(bitwise_and_xor([3, 1, 7], [[1, 1, 3, 3]])) # Expected output: [4]
print(bitwise_and_xor([8, 6, 5, 9, 7, 7, 9, 3, 8], [[3, 5, 5, 5]])) # Expected output: [6]

This function takes in two arguments: A, which is the array of integers, and B, which is the 2D array of queries. It returns an array containing the answers to the queries.

For each query, we initialize the bitwise AND and XOR to the first element in the first range. Then, we iterate through the elements in the first range and compute the bitwise AND and XOR of all the elements in that range. Next, we iterate through the elements in the second range and compute the bitwise AND and XOR of all the elements in that range. Finally, we append the bitwise AND XOR of the two ranges to the result array.



Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
✨ Updates