Queries for Sum of Bitwise AND of all Subarrays in a Range in C++Introduction:In this article, the task is to find the sum of bitwise AND operation results for all possible subarrays within a given range of indices in an array. Bitwise AND is an operation that takes two binary numbers and performs a logical AND operation on each pair of corresponding bits, producing a new binary number. Program:Output: Sum of bitwise AND of all subarrays in the range [0, 2] is: 0 Explanation:
The main goal of this code is to find the sum of bitwise AND operations for all possible subarrays within a given range in an array.
This function takes three parameters: an array (arr), the size of the array (n), and a range specified by indices (left and right). It uses a loop to go through each bit position (0 to 31) since integers are typically represented using 32 bits.
Inside the loop, it counts how many times the bit at the current position is set (1) in the elements of the array within the specified range (left to right).
If the count of set bits at the current position is equal to the number of elements in the range, it means that the AND operation for that bit position will always result in 1 for all subarrays in the range. Therefore, it sets the corresponding bit in the result.
The final result is accumulated by combining the bitwise OR of all the bits.
The main function demonstrates the usage of the sumBitwiseAndInRange function with a sample array ({5, 2, 8}) and a specified range (left = 0, right = 2). It prints the calculated sum of bitwise AND for all subarrays in the specified range. Complexity analysis: Time Complexity:
Space Complexity:
Approach 1: Using observing patternsThe idea behind this approach is to observe the bitwise AND operation for a range of consecutive numbers and realize that the result is the common prefix of their binary representation. After that, we can find this common prefix and convert it back to decimal to get the final result. Program:Output: Sum of bitwise AND of all subarrays in the range [5, 8] is: 0 Explanation: The findCommonPrefix function takes two integers (x and y) and finds the common prefix of their binary representation. It performs this by shifting both numbers to the right until their bits at the current position are different. It then shifts the common prefix back to its original position. The sumBitwiseAndInRange function calls findCommonPrefix to get the common prefix of the binary representation of the given range (left to right). The main function demonstrates the usage of sumBitwiseAndInRange with an example range (left = 5, right = 8). It calculates and prints the sum of bitwise AND for the specified range. Complexity analysis: Time Complexity:
Space Complexity:
Approach 2: Using Bit ManipulationThe idea behind this approach is to perform bitwise AND operation for all elements in the specified range and keep track of the result at each bit position. Program:Output: Sum of bitwise AND of all subarrays in the range [5, 8] is: 0 Explanation:
Complexity analysis: Time Complexity: The time complexity of the sumBitwiseAndInRange function is primarily determined by the loop that iterates through each bit position. The loop runs for a constant number of iterations (32 times in this case), making the time complexity O(1). Space Complexity: The space complexity of the code is very low and constant. The algorithm uses a few integer variables (result, left, right, bites), and the space occupied by these variables does not depend on the input size. The overall time and space complexity for this code is O(1) or constant. Approach 3: Using bitwise shiftIt helps to identify the common leftmost bits of left and right. Set all bits to the right of the common leftmost bits to 0. Program:Output: Sum of bitwise AND of all subarrays in the range [5, 8] is: 0 Explanation:
The function takes two integers, left and right, representing the range. It initializes a variable shift to 0, which will keep track of how many bits to shift. The while loop continues until left and right are equal. In each iteration, both left and right are right-shifted by 1, and shift is incremented. After the loop, it left-shifts either left or right by a shift to restore the common leftmost bits. The final result is returned.
The main function demonstrates the usage of sumBitwiseAndInRange with an example range (left = 5, right = 8). It calculates and prints the sum of bitwise AND for the specified range. Complexity analysis: Time Complexity: The time complexity of the sumBitwiseAndInRange function is primarily determined by the while loop. The loop runs until left and right become equal, which takes logarithmic time in the worst case. The time complexity is O(log(min(left, right))). Space Complexity: The space complexity is very low and constant. The algorithm uses only a few integer variables (left, right, shift), and the space occupied by these variables does not depend on the input size. The space complexity is O(1) or constant. Approach 4: Using Common Prefix BitmaskIt identifies the common leftmost bits of left and right. Set all bits to the right of the common leftmost bits to 0. Use a bitmask to retain the common leftmost bits. Program:Output: Sum of bitwise AND of all subarrays in the range [5, 8] is: 0 Explanation: Function sumBitwiseAndInRange:
Main Function:
Complexity analysis: Time Complexity:
Space Complexity:
Next TopicBasic_istream::peek() method in C++
|