Find all subarray index ranges in given Array with set bit sum equal to X
Given an array arr (1-based indexing) of length N and an integer X, the task is to find and print all index ranges having a set bit sum equal to X in the array.
Examples:
Input: A[] = {1 4 3 5 7}, X = 4
Output: (1, 3), (3, 4)
Explanation: In the above array subarray having set bit sum equal to X (= 4).
Starting from index 1 to 3. {1 4 3} = (001) + (100) + (011) = 4 and
other one is from 3 to 4 {3, 5} = (011) + (101) = 4.Input: arr[] = {5, 3, 0, 4, 10}, X = 7
Output: (1 5)
Explanation: In the above array subarrays having set bit sum equal to X(= 7) start from 1 to 5 only.
Approach: The problem is solved using two pointer approach.
- Write a function countSetBit to count the number of set bits.
- Initialize a counter c=0, to store the individual count for every number in the array.
- Iterate over the array and check for every set bit and increase the counter.
- replace every number with the count of a number of set bits
- Write a function to print a range of subarrays PrintIndex
Run a loop using two pointers i and j and check for the sum as follow:- If the current index sum is less than X then, add the value at arr[j] in currsum
- else if the sum is equal to X push back the start and end index of the array and increment the counter i.
- else decrement the counter, subtract the value at arr[i] from currsum.
- Repeat the same for all elements.
Below is the implementation of the above method :
C++
// C++ program to Find all range// Having set bit sum X in array#include <bits/stdc++.h>using namespace std;// Function to replace elements// With their set bit countvoid countSetBit(vector<int>& arr, int n){ int c = 0, i; for (i = 0; i < n; i++) { int x = arr[i]; while (x) { int l = x % 10; if (x & 1) c++; x /= 2; } // Replace array element // to set bit count arr[i] = c; c = 0; }}// Function to find range of subarrays// having set bit sum equal to X.void PrintIndex(vector<int> arr, int N, int X, vector<int>& v){ int i = 0, j = 0, currSum = arr[0]; while (j < N && i < N) { if (currSum == X) { // push back index i start // point ans end point j // when sum == X v.push_back(i + 1); v.push_back(j + 1); j++; currSum += arr[j]; } // when current sum is // less than X increment j // and add arr[j] else if (currSum < X) { j++; currSum += arr[j]; } // when current sum is // greater than X increment j // and substract arr[i] else { currSum -= arr[i]; i++; } }}// Driver codeint main(){ vector<int> v = { 1, 4, 3, 5, 7 }; int X = 4; int N = v.size(); // replace all the array element into // their set bit count value countSetBit(v, N); vector<int> ans; PrintIndex(v, N, X, ans); for (int i = 0; i < ans.size() - 1; i += 2) cout << "(" << ans[i] << " " << ans[i + 1] << ")" << " "; return 0;} |
Javascript
<script> // JavaScript program to Find all range // Having set bit sum X in array // Function to replace elements // With their set bit count const countSetBit = (arr, n) => { let c = 0, i; for (i = 0; i < n; i++) { let x = arr[i]; while (x) { let l = x % 10; if (x & 1) c++; x = parseInt(x / 2); } // Replace array element // to set bit count arr[i] = c; c = 0; } } // Function to find range of subarrays // having set bit sum equal to X. const PrintIndex = (arr, N, X, v) => { let i = 0, j = 0, currSum = arr[0]; while (j < N && i < N) { if (currSum == X) { // push back index i start // point ans end point j // when sum == X v.push(i + 1); v.push(j + 1); j++; currSum += arr[j]; } // when current sum is // less than X increment j // and add arr[j] else if (currSum < X) { j++; currSum += arr[j]; } // when current sum is // greater than X increment j // and substract arr[i] else { currSum -= arr[i]; i++; } } } // Driver code let v = [1, 4, 3, 5, 7]; let X = 4; let N = v.length; // replace all the array element into // their set bit count value countSetBit(v, N); let ans = []; PrintIndex(v, N, X, ans); for (let i = 0; i < ans.length - 1; i += 2) document.write(`(${ans[i]} ${ans[i + 1]}) `);// This code is contributed by rakeshsahni</script> |
(1 3) (3 4)
Time Complexity: O(N * d) where d is the count of bits in an array element
Auxiliary Space: O(N)


.png)