Javatpoint Logo
Javatpoint Logo

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:

  • Main Goal:

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.

  • Function sumBitwiseAndInRange:

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.

  • Bitwise AND Operation:

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).

  • Set Bits Check:

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.

  • Result Accumulation:

The final result is accumulated by combining the bitwise OR of all the bits.

  • Main Function:

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:

  • The time complexity of the code mainly depends on two loops:
  • The outer loop runs for each bit position (32 times for a 32-bit integer).
  • The inner loop runs for each element in the specified range (left to right).
  • Therefore, the overall time complexity can be expressed as O(32 * (right - left + 1)), which simplifies to O(right - left).

Space Complexity:

  • The space complexity of the code is relatively low and constant. It uses a few integer variables (result, bit, countOnes, i), and the space occupied by these variables does not depend on the input size. Thus, we can consider the space complexity as O(1) or constant.

Approach 1: Using observing patterns

The 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:

  • The time complexity of the findCommonPrefix function is determined by the number of bits in the binary representation of the smaller of the two numbers (x and y). The loop inside findCommonPrefix iterates until the bits at the current position are different, and in the worst case, it takes O(log(min(x, y))) steps.
  • The sumBitwiseAndInRange function calls findCommonPrefix, so its time complexity is also O(log(min(left, right))).
  • The main function and the rest of the code run in constant time because they involve basic operations.
  • The overall time complexity is O(log(min(left, right))).

Space Complexity:

  • The space complexity is very low and constant. The algorithm uses a few integer variables (shift, commonPrefix, left, right, result), and the space occupied by these variables does not depend on the input size.
  • The overall space complexity is O(1) or

Approach 2: Using Bit Manipulation

The 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:

  • The sumBitwiseAndInRange function takes two integers (left and right) representing the range.
  • It initializes the result variable to INT_MAX to represent all bits set to 1.
  • It iterates through each bit position (from the most significant bit to the least significant bit).
  • For each bit position, it checks if the bit is set (1) in both left and right.
  • If the bit is set in both, it sets the corresponding bit in the result.
  • If the bit is not set in both, it clears the corresponding bit in the result.
  • The final result is the sum of bitwise AND for the specified range.
  • 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 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 shift

It 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:

  • Function sumBitwiseAndInRange:

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.

  • Main Function:

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 Bitmask

It 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:

  • The function takes two integers, left and right, representing the range.
  • It initializes a bitmask commonBits to INT_MAX, representing all bits set to 1.
  • The while loop continues until left and right have different common leftmost bits (bits to the left of the rightmost differing bit).
  • In each iteration, the rightmost bit of commonBits is set to 0 using (commonBits - 1).
  • The final result is obtained by performing a bitwise AND between left and commonBits.

Main Function:

  • 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 have different common leftmost bits. In each iteration, the rightmost bit of commonBits is set to 0 using (commonBits - 1). This operation takes constant time.
  • The number of iterations in the while loop is determined by the number of differing bits in the binary representation of left and right. Therefore, 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 (commonBits, left, right), and the space occupied by these variables does not depend on the input size.
  • The space complexity is O(1) or constant.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA