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 subtract 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;} |
Java
// JAVA code to implement the above approachimport 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 countdef 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 codev = [1, 4, 3, 5, 7]X = 4N = len(v)Ā
# replace all the array element into# their set bit count valuecountSetBit(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 arrayusing 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> |
Ā
Ā
(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:
- The code defines a function called countSetBit that takes an integer x and returns the number of set bits in its binary representation.
- 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.
- 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.
- The code enters a while loop with a condition of j < n. This loop iterates through all the elements of the input vector arr.
- 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.
- 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.
- 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.
- The while loop in step 4 continues until j reaches the end of the input vector arr.
- 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 functionif __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) |
(1, 3) (3, 4)
Time complexity: O(n*logx)
Auxiliary Space: O(n)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


