Given an array arr[] consisting of N integers an array Q[][] consisting of queries of the form {l, r}. For each query {l, r}, the task is to determine the minimum sum of all values that must be added to each array element in that range such that the Bitwise AND of all numbers in that range exceeds 0.
Note: Different values can be added to different integers in the given range.
Examples:
Input: arr[] = {1, 2, 4, 8}, Q[][] = {{1, 4}, {2, 3}, {1, 3}}
Output: 3 2 2
Explanation: Binary representation of array elements are as follows:
1 – 0001
2 – 0010
4 – 0100
8 – 1000
For first query {1, 4}, add 1 to all numbers present in the range except the first number. Therefore, Bitwise AND = (1 & 3 & 5 & 9) = 1 and minimum sum of elements added = 3.
For second query {2, 3}, add 2 to 3rd element. Therefore, Bitwise AND = (2 & 6) = 2 and minimum sum of elements added = 2.
For third query {1, 3}, add 1 to 2nd and 3rd elements. Therefore, Bitwise AND = (1 & 3 & 5) = 1 and minimum sum of elements added = 2.Input: arr[] = {4, 6, 5, 3}, Q[][] = {{1, 4}}
Output: 1
Explanation: Optimal way to mmake the Bitwise AND non-zero is to add 1 to the last element. Therefore, bitwise AND = (4 & 6 & 5 & 4) = 4 and minimum sum of elements added = 1.
Approach: The idea is to first observe that the Bitwise AND of all the integers in the range [l, r] can only be non-zero if a bit at a particular index is set for each integer in that range.
Follow the steps below to solve the problem:
- Initialize a 2D vector pre where pre[i][j] stores the minimum sum of integers to be added to all integers from index 0 to index i such that, for each of them, their jth bit is set.
- For each element at index i, check for each of its bit from j = 0 to 31.
- Initialize a variable sum with 0.
- If jth bit is set, update pre[i][j] as pre[i][j]=pre[i-1][j] and increment sum by 2j. Otherwise, update pre[i][j] = pre[i-1][j] + 2j – sum where (2j-sum) is the value that must be added to set the jth bit of arr[i].
- Now, for each query {l, r}, the minimum sum required to set jth bit of all elements in the given range is pre[r][j] – pre[l-1][j].
- For each query {l, r}, find the answer for each bit from j = 0 to 31 and print the minimum amongst them.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the min sum required // to set the jth bit of all integer void processing(vector<int> v, vector<vector<long long int> >& pre) { // Number of elements int N = v.size(); // Traverse all elements for (int i = 0; i < N; i++) { // Binary representation bitset<32> b(v[i]); long long int sum = 0; // Take previous values if (i != 0) { pre[i] = pre[i - 1]; } // Processing each bit and // store it in 2d vector for (int j = 0; j < 32; j++) { if (b[j] == 1) { sum += 1ll << j; } else { pre[i][j] += (1ll << j) - sum; } } } } // Function to print the minimum // sum for each query long long intprocess_query(vector<vector<int> > Q, vector<vector<long long int> >& pre) { // Stores the sum for each query vector<int> ans; for (int i = 0; i < Q.size(); i++) { // Update wrt 0-based index --Q[i][0], --Q[i][1]; // Initizlize answer long long int min1 = INT_MAX; // Find minimum sum for each bit if (Q[i][0] == 0) { for (int j = 0; j < 32; j++) { min1 = min(pre[Q[i][1]][j], min1); } } else { for (int j = 0; j < 32; j++) { min1 = min(pre[Q[i][1]][j] - pre[Q[i][0] - 1][j], min1); } } // Store the answer for // each query ans.push_back(min1); } // Print the answer vector for (int i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } } // Driver Code int main() { // Given array vector<int> arr = { 1, 2, 4, 8 }; // Given Queries vector<vector<int> > Q = { { 1, 4 }, { 2, 3 }, { 1, 3 } }; // 2d Prefix vector vector<vector<long long int> > pre( 100001, vector<long long int>(32, 0)); // Preprocessing processing(arr, pre); // Function call for queries process_query(Q, pre); return 0; } |
3 2 2
Time Complexity: O(N*32 + sizeof(Q)*32)
Auxiliary Space: O(N*32)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Minimize sum of prime numbers added to make an array non-decreasing
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Minimize elements to be added to a given array such that it contains another given array as its subsequence
- Minimize elements to be added to a given array such that it contains another given array as its subsequence | Set 2
- Choose points from two ranges such that no point lies in both the ranges
- Minimize bits to be flipped in X and Y such that their Bitwise OR is equal to Z
- Minimize the sum calculated by repeatedly removing any two elements and inserting their sum to the Array
- Queries for maximum and minimum difference between Fibonacci numbers in given ranges
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Minimum value of X that can be added to N to minimize sum of the digits to ≤ K
- Queries on probability of even or odd number in given ranges
- Queries for maximum difference between prime numbers in given ranges
- Minimum number to be added to minimize LCM of two given numbers
- Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum
- Count of pairs from Array with sum equal to twice their bitwise AND
- Queries to check if a number lies in N ranges of L-R
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- Queries for bitwise AND in the index range [L, R] of the given array
- Smallest number to be added in first Array modulo M to make frequencies of both Arrays equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

