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 count
void 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 subtract arr[i]
Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā currSum -= arr[i];
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i++;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
}
Ā 
// Driver code
int 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;
}


Java




// JAVA code to implement the above approach
import java.util.*;
class GFG {
Ā 
Ā Ā // Function to replace elements
Ā Ā // With their set bit count
Ā Ā static void countSetBit(int[] arr, int n)
Ā Ā {
Ā Ā Ā Ā int c = 0, i;
Ā 
Ā Ā Ā Ā for (i = 0; i < n; i++) {
Ā 
Ā Ā Ā Ā Ā Ā int x = arr[i];
Ā Ā Ā Ā Ā Ā while (x > 0) {
Ā Ā Ā Ā Ā Ā Ā Ā int l = x % 10;
Ā Ā Ā Ā Ā Ā Ā Ā if ((x & 1) == 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.
Ā Ā static void PrintIndex(int[] arr, int N, int X,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ArrayList<Integer> 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.add(i + 1);
Ā Ā Ā Ā Ā Ā Ā Ā v.add(j + 1);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā j++;
Ā Ā Ā Ā Ā Ā Ā Ā if (j < N)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā currSum += arr[j];
Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā // when current sum is
Ā Ā Ā Ā Ā Ā // less than X increment j
Ā Ā Ā Ā Ā Ā // and add arr[j]
Ā Ā Ā Ā Ā Ā else if (currSum < X) {
Ā Ā Ā Ā Ā Ā Ā Ā j++;
Ā Ā Ā Ā Ā Ā Ā Ā if (j < N)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā currSum += arr[j];
Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā // when current sum is
Ā Ā Ā Ā Ā Ā // greater than X increment j
Ā Ā Ā Ā Ā Ā // and subtract arr[i]
Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā currSum -= arr[i];
Ā Ā Ā Ā Ā Ā Ā Ā i++;
Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā Ā }
Ā 
Ā Ā // Driver Code
Ā Ā public static void main(String[] args)
Ā Ā {
Ā Ā Ā Ā int[] v = { 1, 4, 3, 5, 7 };
Ā Ā Ā Ā int X = 4;
Ā Ā Ā Ā int N = v.length;
Ā 
Ā Ā Ā Ā // replace all the array element into
Ā Ā Ā Ā // their set bit count value
Ā Ā Ā Ā countSetBit(v, N);
Ā 
Ā Ā Ā Ā ArrayList<Integer> ans = newĀ  ArrayList<Integer>();
Ā 
Ā Ā Ā Ā PrintIndex(v, N, X, ans);
Ā 
Ā Ā Ā Ā for (int i = 0; i < ans.size() - 1; i += 2)
Ā Ā Ā Ā Ā Ā System.out.print("(" + ans.get(i) + " " + ans.get(i + 1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + ")"
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + " ");
Ā Ā }
}
Ā 
// This code is contributed by sanjoy_62.


Python3




# Python program to Find all range
# Having set bit sum X in array
Ā 
# Function to replace elements
# With their set bit count
def countSetBit(arr, n):
Ā Ā Ā Ā c = 0
Ā 
Ā Ā Ā Ā for i in range(n):
Ā Ā Ā Ā Ā Ā Ā Ā x = arr[i]
Ā Ā Ā Ā Ā Ā Ā Ā while (x):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā l = x % 10
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (x & 1):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā c += 1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā x = 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.
def PrintIndex(arr, N, X, v):
Ā 
Ā Ā Ā Ā i,j,currSum = 0,0,arr[0]
Ā 
Ā Ā Ā Ā while (j < N and i < N):
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if (currSum == X):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # append back index i start
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # point ans end point j
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # when sum == X
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā v.append(i + 1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā v.append(j + 1)
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j += 1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if(j<N):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā currSum += arr[j]
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # when current sum is
Ā Ā Ā Ā Ā Ā Ā Ā # less than X increment j
Ā Ā Ā Ā Ā Ā Ā Ā # and add arr[j]
Ā Ā Ā Ā Ā Ā Ā Ā elif (currSum < X):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j += 1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if(j<N):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā currSum += arr[j]
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # when current sum is
Ā Ā Ā Ā Ā Ā Ā Ā # greater than X increment j
Ā Ā Ā Ā Ā Ā Ā Ā # and subtract arr[i]
Ā Ā Ā Ā Ā Ā Ā Ā else:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā currSum -= arr[i]
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i += 1
Ā 
# Driver code
v = [1, 4, 3, 5, 7]
X = 4
N = len(v)
Ā 
# replace all the array element into
# their set bit count value
countSetBit(v, N)
ans = []
PrintIndex(v, N, X, ans)
Ā 
for i in range(0,len(ans) - 1,2):
Ā Ā Ā Ā print(f"({ans[i]} {ans[i + 1]})",end=" ")
Ā 
# This code is contributed by shinjanpatra


C#




// C# program to Find all range
// Having set bit sum XĀ  in array
using System;
using System.Collections;
Ā 
class GFG {
Ā 
Ā Ā // Function to replace elements
Ā Ā // With their set bit count
Ā Ā static void countSetBit(int[] arr, int n)
Ā Ā {
Ā Ā Ā Ā int c = 0, i;
Ā 
Ā Ā Ā Ā for (i = 0; i < n; i++) {
Ā 
Ā Ā Ā Ā Ā Ā int x = arr[i];
Ā Ā Ā Ā Ā Ā while (x > 0) {
Ā Ā Ā Ā Ā Ā Ā Ā int l = x % 10;
Ā Ā Ā Ā Ā Ā Ā Ā if ((x & 1) == 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.
Ā Ā static void PrintIndex(int[] arr, int N, int X,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ArrayList 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.Add(i + 1);
Ā Ā Ā Ā Ā Ā Ā Ā v.Add(j + 1);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā j++;
Ā Ā Ā Ā Ā Ā Ā Ā if (j < N)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā currSum += arr[j];
Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā // when current sum is
Ā Ā Ā Ā Ā Ā // less than X increment j
Ā Ā Ā Ā Ā Ā // and add arr[j]
Ā Ā Ā Ā Ā Ā else if (currSum < X) {
Ā Ā Ā Ā Ā Ā Ā Ā j++;
Ā Ā Ā Ā Ā Ā Ā Ā if (j < N)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā currSum += arr[j];
Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā // when current sum is
Ā Ā Ā Ā Ā Ā // greater than X increment j
Ā Ā Ā Ā Ā Ā // and subtract arr[i]
Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā currSum -= arr[i];
Ā Ā Ā Ā Ā Ā Ā Ā i++;
Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā Ā }
Ā 
Ā Ā // Driver code
Ā Ā public static void Main()
Ā Ā {
Ā Ā Ā Ā int[] v = { 1, 4, 3, 5, 7 };
Ā Ā Ā Ā int X = 4;
Ā Ā Ā Ā int N = v.Length;
Ā 
Ā Ā Ā Ā // replace all the array element into
Ā Ā Ā Ā // their set bit count value
Ā Ā Ā Ā countSetBit(v, N);
Ā 
Ā Ā Ā Ā ArrayList ans = new ArrayList();
Ā 
Ā Ā Ā Ā PrintIndex(v, N, X, ans);
Ā 
Ā Ā Ā Ā for (int i = 0; i < ans.Count - 1; i += 2)
Ā Ā Ā Ā Ā Ā Console.Write("(" + ans[i] + " " + ans[i + 1]
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + ")"
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + " ");
Ā Ā }
}
Ā 
// This code is contributed by Samim Hossain Mondal.


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 subtract 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>


Ā 
Ā 

Output

(1 3) (3 4)






Time Complexity: O(N * d) where d is the count of bits in an array element
Auxiliary Space: O(N)

Another Approach:

  1. The code defines a function called countSetBit that takes an integer x and returns the number of set bits in its binary representation.
  2. The code also defines another function called printSubarraysWithSetBitSumX that takes a vector of integers arr and an integer X. This function prints all the subarrays of arr whose sum of set bits is equal to X.
  3. Inside the printSubarraysWithSetBitSumX function, the code initializes some variables: n is the size of the input vector arr, i and j are two pointers initially set to 0, and currSum is the current sum of set bits.
  4. The code enters a while loop with a condition of j < n. This loop iterates through all the elements of the input vector arr.
  5. Inside the while loop, the code adds the count of set bits of the current element to the currSum variable and increments j by 1.
  6. The code then enters another while loop with a condition of currSum > X. This loop removes the set bit count of the element pointed by i from the currSum variable and increments i by 1 until the currSum becomes less than or equal to X.
  7. If the currSum is equal to X after the above while loop, the code prints the indices of the subarray whose sum of set bits is equal to X.
  8. The while loop in step 4 continues until j reaches the end of the input vector arr.
  9. Finally, the main function creates a vector arr containing integers {1, 4, 3, 5, 7} and sets X to 4. It then calls the printSubarraysWithSetBitSumX function with these arguments, which prints ā€œ(1, 3) (3, 4)ā€ to the console.

Below is the implementation of the above approach:

C++




#include <iostream>
#include <vector>
using namespace std;
Ā 
int countSetBit(int x) {
Ā Ā Ā Ā int count = 0;
Ā Ā Ā Ā while (x > 0) {
Ā Ā Ā Ā Ā Ā Ā Ā count += x & 1;
Ā Ā Ā Ā Ā Ā Ā Ā x >>= 1;
Ā Ā Ā Ā }
Ā Ā Ā Ā return count;
}
Ā 
void printSubarraysWithSetBitSumX(vector<int>& arr, int X) {
Ā Ā Ā Ā int n = arr.size();
Ā Ā Ā Ā int i = 0, j = 0, currSum = 0;
Ā Ā Ā Ā while (j < n) {
Ā Ā Ā Ā Ā Ā Ā Ā currSum += countSetBit(arr[j]);
Ā Ā Ā Ā Ā Ā Ā Ā j++;
Ā Ā Ā Ā Ā Ā Ā Ā while (currSum > X) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā currSum -= countSetBit(arr[i]);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i++;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā if (currSum == X) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cout << "(" << i + 1 << ", " << j << ") ";
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
}
Ā 
int main() {
Ā Ā Ā Ā vector<int> arr = {1, 4, 3, 5, 7};
Ā Ā Ā Ā int X = 4;
Ā Ā Ā Ā printSubarraysWithSetBitSumX(arr, X); // prints (1, 3) (3, 4)
Ā Ā Ā Ā return 0;
}


Java




import java.util.*;
Ā 
public class SubarraysWithSetBitSum {
Ā Ā Ā Ā public static int countSetBit(int x) {
Ā Ā Ā Ā Ā Ā Ā Ā int count = 0;
Ā Ā Ā Ā Ā Ā Ā Ā while (x > 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count += x & 1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā x >>= 1;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā return count;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā public static void printSubarraysWithSetBitSumX(ArrayList<Integer> arr, int X) {
Ā Ā Ā Ā Ā Ā Ā Ā int n = arr.size();
Ā Ā Ā Ā Ā Ā Ā Ā int i = 0, j = 0, currSum = 0;
Ā Ā Ā Ā Ā Ā Ā Ā while (j < n) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā currSum += countSetBit(arr.get(j));
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j++;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (currSum > X) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā currSum -= countSetBit(arr.get(i));
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i++;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (currSum == X) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.print("(" + (i + 1) + ", " + j + ") ");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā public static void main(String[] args) {
Ā Ā Ā Ā Ā Ā Ā Ā ArrayList<Integer> arr = new ArrayList<Integer>(Arrays.asList(1, 4, 3, 5, 7));
Ā Ā Ā Ā Ā Ā Ā Ā int X = 4;
Ā Ā Ā Ā Ā Ā Ā Ā printSubarraysWithSetBitSumX(arr, X); // prints (1, 3) (3, 4)
Ā Ā Ā Ā }
}


Python3




def count_set_bits(x):
Ā Ā Ā Ā # Function to count the number of set bits (1s) in a binary representation of 'x'
Ā Ā Ā Ā count = 0
Ā Ā Ā Ā while x > 0:
Ā Ā Ā Ā Ā Ā Ā Ā count += x & 1Ā  # Add the least significant bit of 'x' to the count
Ā Ā Ā Ā Ā Ā Ā Ā x >>= 1Ā  # Right shift 'x' to check the next bit
Ā Ā Ā Ā return count
Ā 
def print_subarrays_with_set_bit_sum_x(arr, X):
Ā Ā Ā Ā n = len(arr)Ā  # Get the length of the input array 'arr'
Ā Ā Ā Ā i, j, curr_sum = 0, 0, 0Ā  # Initialize variables for subarray tracking
Ā 
Ā Ā Ā Ā while j < n:Ā  # Iterate through the array using a sliding window (j moves forward)
Ā Ā Ā Ā Ā Ā Ā Ā curr_sum += count_set_bits(arr[j])Ā  # Add the count of set bits in 'arr[j]' to 'curr_sum'
Ā Ā Ā Ā Ā Ā Ā Ā j += 1
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā while curr_sum > X:Ā  # If 'curr_sum' exceeds the target 'X', move the window's left end (i) forward
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr_sum -= count_set_bits(arr[i])Ā  # Subtract the count of set bits in 'arr[i]' from 'curr_sum'
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i += 1
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if curr_sum == X:Ā  # If 'curr_sum' matches the target 'X', print the subarray
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā print(f"({i + 1}, {j}) ", end='')
Ā 
# Main function
if __name__ == "__main__":
Ā Ā Ā Ā arr = [1, 4, 3, 5, 7]Ā  # Input array
Ā Ā Ā Ā X = 4Ā  # Target sum of set bits
Ā Ā Ā Ā print_subarrays_with_set_bit_sum_x(arr, X)Ā  # Call the function to find and print subarrays with the target sum


C#




using System;
using System.Collections.Generic;
Ā 
public class SubarraysWithSetBitSum {
Ā Ā Ā Ā // Function to count the number of set bits in a number
Ā Ā Ā Ā public static int CountSetBit(int x) {
Ā Ā Ā Ā Ā Ā Ā Ā int count = 0;
Ā Ā Ā Ā Ā Ā Ā Ā while (x > 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count += x & 1; // Increment count if the last bit is 1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā x >>= 1; // Right shift the number to check the next bit
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā return count;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to find and print subarrays with a given set bit sum
Ā Ā Ā Ā public static void PrintSubarraysWithSetBitSumX(List<int> arr, int X) {
Ā Ā Ā Ā Ā Ā Ā Ā int n = arr.Count;
Ā Ā Ā Ā Ā Ā Ā Ā int i = 0, j = 0, currSum = 0;
Ā Ā Ā Ā Ā Ā Ā Ā while (j < n) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā currSum += CountSetBit(arr[j]); // Calculate set bit sum for the current element
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j++;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Slide the window to the right while current set bit sum is greater than X
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (currSum > X) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā currSum -= CountSetBit(arr[i]); // Remove set bit count of the left element
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i++;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If the current set bit sum matches X, print the subarray indices
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (currSum == X) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.Write("(" + (i + 1) + ", " + j + ") ");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā public static void Main(string[] args) {
Ā Ā Ā Ā Ā Ā Ā Ā List<int> arr = new List<int> { 1, 4, 3, 5, 7 };
Ā Ā Ā Ā Ā Ā Ā Ā int X = 4;
Ā Ā Ā Ā Ā Ā Ā Ā PrintSubarraysWithSetBitSumX(arr, X); // prints (1, 3) (3, 4)
Ā Ā Ā Ā }
}


Javascript




function countSetBit(x) {
Ā Ā Ā Ā let count = 0;
Ā Ā Ā Ā while (x > 0) {
Ā Ā Ā Ā Ā Ā Ā Ā count += x & 1;
Ā Ā Ā Ā Ā Ā Ā Ā x >>= 1;
Ā Ā Ā Ā }
Ā Ā Ā Ā return count;
}
Ā 
function printSubarraysWithSetBitSumX(arr, X) {
Ā Ā Ā Ā const n = arr.length;
Ā Ā Ā Ā let i = 0, j = 0, currSum = 0;
Ā Ā Ā Ā while (j < n) {
Ā Ā Ā Ā Ā Ā Ā Ā currSum += countSetBit(arr[j]);
Ā Ā Ā Ā Ā Ā Ā Ā j++;
Ā Ā Ā Ā Ā Ā Ā Ā while (currSum > X) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā currSum -= countSetBit(arr[i]);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i++;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā if (currSum === X) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā console.log(`(${i + 1}, ${j})`);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
}
Ā 
const arr = [1, 4, 3, 5, 7];
const X = 4;
printSubarraysWithSetBitSumX(arr, X); // prints (1, 3) (3, 4)


Output

(1, 3) (3, 4) 






Time complexity: O(n*logx)
Auxiliary Space: O(n)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button