Queries for number of array elements in a range with Kth Bit Set
Last Updated :
29 Mar, 2024
Given an array of N positive (32-bit)integers, the task is to answer Q queries of the following form:
Query(L, R, K): Print the number of elements of the array in the
range L to R, which have their Kth bit as set
Note: Consider LSB to be indexed at 1.
Examples:
Input : arr[] = { 8, 9, 1, 3 }
Query 1: L = 1, R = 3, K = 4
Query 2: L = 2, R = 4, K = 1
Output :
2
3
Explanation:
For the 1st query, the range (1, 3) represents elements, {8, 9, 1}. Among these elements only 8 and 9 have their 4th bit set. Thus, the answer for this query is 2.
For the 2nd query, the range (2, 4) represents elements, {9, 1, 3}. All of these elements have their 1st bit set. Thus, the answer for this query is 3.
Prerequisites: Bit Manipulation | Prefix Sum Arrays
Method 1 (Brute Force) : For each query, traverse the array from L to R, and at every index check if the array element at that index has its Kth bit as set. If it does increment the counter variable.
Below is the implementation of above approach.
C++
/* C++ Program to find the number of elements
in a range L to R having the Kth bit as set */
#include <bits/stdc++.h>
using namespace std;
// Maximum bits required in binary representation
// of an array element
#define MAX_BITS 32
/* Returns true if n has its kth bit as set,
else returns false */
bool isKthBitSet(int n, int k)
{
if (n & (1 << (k - 1)))
return true;
return false;
}
/* Returns the answer for each query with range L
to R querying for the number of elements with
the Kth bit set in the range */
int answerQuery(int L, int R, int K, int arr[])
{
// counter stores the number of element in
// the range with the kth bit set
int counter = 0;
for (int i = L; i <= R; i++) {
if (isKthBitSet(arr[i], K)) {
counter++;
}
}
return counter;
}
// Print the answer for all queries
void answerQueries(int queries[][3], int Q,
int arr[], int N)
{
int query_L, query_R, query_K;
for (int i = 0; i < Q; i++) {
query_L = queries[i][0] - 1;
query_R = queries[i][1] - 1;
query_K = queries[i][2];
cout << "Result for Query " << i + 1 << " = "
<< answerQuery(query_L, query_R, query_K, arr)
<< endl;
}
}
// Driver Code
int main()
{
int arr[] = { 8, 9, 1, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
/* queries[][] denotes the array of queries
where each query has three integers
query[i][0] -> Value of L for ith query
query[i][0] -> Value of R for ith query
query[i][0] -> Value of K for ith query */
int queries[][3] = {
{ 1, 3, 4 },
{ 2, 4, 1 }
};
int Q = sizeof(queries) / sizeof(queries[0]);
answerQueries(queries, Q, arr, N);
return 0;
}
Java
// Java Program to find the
// number of elements in a
// range L to R having the
// Kth bit as set
import java.util.*;
import java.lang.*;
import java.io.*;
// Maximum bits required
// in binary representation
// of an array element
class GFG
{
static final int MAX_BITS = 32;
/* Returns true if n
has its kth bit as set,
else returns false */
static boolean isKthBitSet(int n,
int k)
{
if ((n & (1 << (k - 1))) != 0)
return true;
return false;
}
/* Returns the answer for
each query with range L
to R querying for the number
of elements with the Kth bit
set in the range */
static int answerQuery(int L, int R,
int K, int arr[])
{
// counter stores the number
// of element in the range
// with the kth bit set
int counter = 0;
for (int i = L; i <= R; i++)
{
if (isKthBitSet(arr[i], K))
{
counter++;
}
}
return counter;
}
// Print the answer
// for all queries
static void answerQueries(int queries[][], int Q,
int arr[], int N)
{
int query_L, query_R, query_K;
for (int i = 0; i < Q; i++)
{
query_L = queries[i][0] - 1;
query_R = queries[i][1] - 1;
query_K = queries[i][2];
System.out.println("Result for Query " +
(i + 1) + " = " +
answerQuery(query_L, query_R,
query_K, arr));
}
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 8, 9, 1, 3 };
int N = arr.length;
/* queries[][] denotes the array
of queries where each query has
three integers
query[i][0] -> Value of L for ith query
query[i][0] -> Value of R for ith query
query[i][0] -> Value of K for ith query */
int queries[][] =
{
{ 1, 3, 4 },
{ 2, 4, 1 }
};
int Q = queries.length;
answerQueries(queries, Q, arr, N);
}
}
// This code is contributed
// by Subhadeep
Python3
# Python3 Program to find the number of elements
# in a range L to R having the Kth bit as set
# Maximum bits required in binary representation
# of an array element
MAX_BITS = 32
# Returns true if n has its kth bit as set,
# else returns false
def isKthBitSet(n, k):
if (n & (1 << (k - 1))):
return True
return False
# Returns the answer for each query with range L
# to R querying for the number of elements with
# the Kth bit set in the range
def answerQuery(L, R, K, arr):
# counter stores the number of element
# in the range with the kth bit set
counter = 0
for i in range(L, R + 1):
if (isKthBitSet(arr[i], K)):
counter += 1
return counter
# Print the answer for all queries
def answerQueries(queries, Q, arr, N):
for i in range(Q):
query_L = queries[i][0] - 1
query_R = queries[i][1] - 1
query_K = queries[i][2]
print("Result for Query", i + 1, "=",
answerQuery(query_L, query_R,
query_K, arr))
# Driver Code
if __name__ == "__main__":
arr = [ 8, 9, 1, 3 ]
N = len(arr)
# queries[][] denotes the array of queries
# where each query has three integers
# query[i][0] -> Value of L for ith query
# query[i][0] -> Value of R for ith query
# query[i][0] -> Value of K for ith query
queries = [[ 1, 3, 4 ],
[ 2, 4, 1 ]]
Q = len(queries)
answerQueries(queries, Q, arr, N)
# This code is contributed by ita_c
C#
// C# Program to find the number of
// elements in a range L to R having
// the Kth bit as set
using System;
// Maximum bits required
// in binary representation
// of an array element
class GFG
{
static readonly int MAX_BITS = 32;
/* Returns true if n
has its kth bit as set,
else returns false */
static bool isKthBitSet(int n,
int k)
{
if ((n & (1 << (k - 1))) != 0)
return true;
return false;
}
/* Returns the answer for each query
with range L to R querying for the
number of elements with the Kth bit
set in the range */
static int answerQuery(int L, int R,
int K, int []arr)
{
// counter stores the number
// of element in the range
// with the kth bit set
int counter = 0;
for (int i = L; i <= R; i++)
{
if (isKthBitSet(arr[i], K))
{
counter++;
}
}
return counter;
}
// Print the answer for all queries
static void answerQueries(int [,]queries, int Q,
int []arr, int N)
{
int query_L, query_R, query_K;
for (int i = 0; i < Q; i++)
{
query_L = queries[i,0] - 1;
query_R = queries[i,1] - 1;
query_K = queries[i,2];
Console.WriteLine("Result for Query " +
(i + 1) + " = " +
answerQuery(query_L, query_R,
query_K, arr));
}
}
// Driver Code
public static void Main()
{
int []arr = { 8, 9, 1, 3 };
int N = arr.Length;
/* queries[][] denotes the array
of queries where each query has
three integers
query[i][0] -> Value of L for ith query
query[i][0] -> Value of R for ith query
query[i][0] -> Value of K for ith query */
int [,]queries = { { 1, 3, 4 },
{ 2, 4, 1 } };
int Q = queries.GetLength(0);
answerQueries(queries, Q, arr, N);
}
}
// This code is contributed
// by 29AjayKumar
JavaScript
<script>
// Javascript Program to find the
// number of elements in a
// range L to R having the
// Kth bit as set
// Maximum bits required
// in binary representation
// of an array element
let MAX_BITS = 32;
/* Returns true if n
has its kth bit as set,
else returns false */
function isKthBitSet(n,k)
{
if ((n & (1 << (k - 1))) != 0)
return true;
return false;
}
/* Returns the answer for
each query with range L
to R querying for the number
of elements with the Kth bit
set in the range */
function answerQuery(L,R,K,arr)
{
// counter stores the number
// of element in the range
// with the kth bit set
let counter = 0;
for (let i = L; i <= R; i++)
{
if (isKthBitSet(arr[i], K))
{
counter++;
}
}
return counter;
}
// Print the answer
// for all queries
function answerQueries(queries,Q,arr,N)
{
let query_L, query_R, query_K;
for (let i = 0; i < Q; i++)
{
query_L = queries[i][0] - 1;
query_R = queries[i][1] - 1;
query_K = queries[i][2];
document.write("Result for Query " +
(i + 1) + " = " +
answerQuery(query_L, query_R,
query_K, arr)+"<br>");
}
}
// Driver Code
let arr=[8, 9, 1, 3];
let N = arr.length;
/* queries[][] denotes the array
of queries where each query has
three integers
query[i][0] -> Value of L for ith query
query[i][0] -> Value of R for ith query
query[i][0] -> Value of K for ith query */
let queries =
[
[ 1, 3, 4 ],
[ 2, 4, 1 ]
];
let Q = queries.length;
answerQueries(queries, Q, arr, N);
// This code is contributed by unknown2108
</script>
Output: Result for Query 1 = 2
Result for Query 2 = 3
Time Complexity : O(N) for each query.
Method 2 (Efficient) : Assuming that every integer in the array has at max 32 bits in its Binary Representation. A 2D prefix sum array can be built to solve the problem. Here the 2nd dimension of the prefix array is of size equal to the maximum number of bits required to represent a integer of the array in binary.
Let the Prefix Sum Array be P[][]. Now, P[i][j] denotes the number of Elements from 0 to i, which have their jth bit as set. This prefix sum array is built before answering the queries. If a query from L to R is encountered, querying for elements in this range having their Kth bit as set, then the answer for that query is P[R][K] - P[L - 1][K].
Below is the implementation of above approach.
C++
/* C++ Program to find the number of elements
in a range L to R having the Kth bit as set */
#include <bits/stdc++.h>
using namespace std;
// Maximum bits required in binary representation
// of an array element
#define MAX_BITS 32
/* Returns true if n has its kth bit as set,
else returns false */
bool isKthBitSet(int n, int k)
{
if (n & (1 << (k - 1)))
return true;
return false;
}
// Return pointer to the prefix sum array
int** buildPrefixArray(int N, int arr[])
{
// Build a prefix sum array P[][]
// where P[i][j] represents the number of
// elements from 0 to i having the jth bit as set
int** P = new int*[N + 1];
for (int i = 0; i <= N; ++i) {
P[i] = new int[MAX_BITS + 1];
}
for (int i = 0; i <= MAX_BITS; i++) {
P[0][i] = 0;
}
for (int i = 0; i < N; i++) {
for (int j = 1; j <= MAX_BITS; j++) {
// prefix sum from 0 to i for each bit
// position jhas the value of sum from 0
// to i-1 for each j
if (i)
P[i][j] = P[i - 1][j];
// if jth bit set then increment P[i][j] by 1
bool isJthBitSet = isKthBitSet(arr[i], j);
if (isJthBitSet) {
P[i][j]++;
}
}
}
return P;
}
/* Returns the answer for each query with range
L to R querying for the number of elements with
the Kth bit set in the range */
int answerQuery(int L, int R, int K, int** P)
{
/* Number of elements in range L to R with Kth
bit set = (Number of elements from 0 to R with
kth bit set) - (Number of elements from 0 to L-1
with kth bit set) */
if (L)
return P[R][K] - P[L - 1][K];
else
return P[R][K];
}
// Print the answer for all queries
void answerQueries(int queries[][3], int Q,
int arr[], int N)
{
// Build Prefix Array to answer queries efficiently
int** P = buildPrefixArray(N, arr);
int query_L, query_R, query_K;
for (int i = 0; i < Q; i++) {
query_L = queries[i][0] - 1;
query_R = queries[i][1] - 1;
query_K = queries[i][2];
cout << "Result for Query " << i + 1 << " = "
<< answerQuery(query_L, query_R, query_K, P)
<< endl;
}
}
// Driver Code
int main()
{
int arr[] = { 8, 9, 1, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
/* queries[][] denotes the array of queries
where each query has three integers
query[i][0] -> Value of L for ith query
query[i][0] -> Value of R for ith query
query[i][0] -> Value of K for ith query */
int queries[][3] = {
{ 1, 3, 4 },
{ 2, 4, 1 }
};
int Q = sizeof(queries) / sizeof(queries[0]);
answerQueries(queries, Q, arr, N);
return 0;
}
Java
/* Java Program to find the number of elements
in a range L to R having the Kth bit as set */
import java.io.*;
class GFG
{
// Maximum bits required in binary representation
// of an array element
static int MAX_BITS = 32;
/* Returns true if n has its kth bit as set,
else returns false */
static boolean isKthBitSet(int n, int k)
{
if((n & (1 << (k - 1))) != 0)
{
return true;
}
return false;
}
// Return pointer to the prefix sum array
static int[][] buildPrefixArray(int N, int[] arr)
{
// Build a prefix sum array P[][]
// where P[i][j] represents the number of
// elements from 0 to i having the jth bit as set
int[][] P = new int[N + 1][MAX_BITS + 1];
for(int i = 0; i <= MAX_BITS; i++)
{
P[0][i] = 0;
}
for(int i = 0; i < N; i++)
{
for(int j = 1; j <= MAX_BITS; j++)
{
// prefix sum from 0 to i for each bit
// position jhas the value of sum from 0
// to i-1 for each j
if(i != 0)
{
P[i][j] = P[i - 1][j];
}
// if jth bit set then increment P[i][j] by 1
boolean isJthBitSet = isKthBitSet(arr[i], j);
if(isJthBitSet)
{
P[i][j]++;
}
}
}
return P;
}
/* Returns the answer for each query with range
L to R querying for the number of elements with
the Kth bit set in the range */
static int answerQuery(int L, int R, int K, int[][] P)
{
/* Number of elements in range L to R with Kth
bit set = (Number of elements from 0 to R with
kth bit set) - (Number of elements from 0 to L-1
with kth bit set) */
if(L != 0)
{
return P[R][K] - P[L - 1][K];
}
else
{
return P[R][K];
}
}
// Print the answer for all queries
static void answerQueries(int[][] queries,int Q,
int[] arr, int N)
{
// Build Prefix Array to answer queries efficiently
int[][] P = buildPrefixArray(N, arr);
int query_L, query_R, query_K;
for(int i = 0; i < Q; i++)
{
query_L = queries[i][0] - 1;
query_R = queries[i][1] - 1;
query_K = queries[i][2];
System.out.println("Result for Query " + (i + 1) + " = " + answerQuery(query_L, query_R, query_K, P));
}
}
// Driver Code
public static void main (String[] args)
{
int[] arr = {8, 9, 1, 3};
int N = arr.length;
/* queries[][] denotes the array of queries
where each query has three integers
query[i][0] -> Value of L for ith query
query[i][0] -> Value of R for ith query
query[i][0] -> Value of K for ith query */
int[][] queries = {{1, 3, 4},{2, 4, 1}};
int Q = queries.length;
answerQueries(queries, Q, arr, N);
}
}
// This code is contributed by avanitrachhadiya2155
Python3
# Python3 program to find the number
# of elements in a range L to R having
# the Kth bit as set
# Maximum bits required in binary
# representation of an array element
MAX_BITS = 32
# Returns true if n has its kth
# bit as set,else returns false
def isKthBitSet(n, k):
if (n & (1 << (k - 1))):
return True
return False
# Return pointer to the prefix sum array
def buildPrefixArray(N, arr):
# Build a prefix sum array P[][]
# where P[i][j] represents the
# number of elements from 0 to
# i having the jth bit as set
P = [[0 for i in range(MAX_BITS + 1)]
for i in range(N + 1)]
for i in range(N):
for j in range(1, MAX_BITS + 1):
# prefix sum from 0 to i for each bit
# position jhas the value of sum from 0
# to i-1 for each j
if (i):
P[i][j] = P[i - 1][j]
# If jth bit set then increment
# P[i][j] by 1
isJthBitSet = isKthBitSet(arr[i], j)
if (isJthBitSet):
P[i][j] += 1
return P
# Returns the answer for each query
# with range L to R querying for the
# number of elements with the Kth bit
# set in the range
def answerQuery(L, R, K, P):
# Number of elements in range L to
# R with Kth bit set = (Number of
# elements from 0 to R with kth
# bit set) - (Number of elements
# from 0 to L-1 with kth bit set)
if (L):
return P[R][K] - P[L - 1][K]
else:
return P[R][K]
# Print the answer for all queries
def answerQueries(queries, Q, arr, N):
# Build Prefix Array to answer
# queries efficiently
P = buildPrefixArray(N, arr)
for i in range(Q):
query_L = queries[i][0] - 1
query_R = queries[i][1] - 1
query_K = queries[i][2]
print("Result for Query ", i + 1,
" = ", answerQuery(query_L, query_R,
query_K, P))
# Driver Code
if __name__ == '__main__':
arr = [ 8, 9, 1, 3 ]
N = len(arr)
# queries[]denotes the array of queries
# where each query has three integers
# query[i][0] -> Value of L for ith query
# query[i][0] -> Value of R for ith query
# query[i][0] -> Value of K for ith query
queries = [ [ 1, 3, 4 ],
[ 2, 4, 1 ] ]
Q = len(queries)
answerQueries(queries, Q, arr, N)
# This code is contributed by mohit kumar 29
C#
// C# program to find the number of elements
// in a range L to R having the Kth bit as set
using System;
class GFG{
// Maximum bits required in binary representation
// of an array element
static int MAX_BITS = 32;
// Returns true if n has its kth bit as set,
// else returns false
static bool isKthBitSet(int n, int k)
{
if ((n & (1 << (k - 1))) != 0)
{
return true;
}
return false;
}
// Return pointer to the prefix sum array
static int[,] buildPrefixArray(int N, int[] arr)
{
// Build a prefix sum array P[][]
// where P[i][j] represents the
// number of elements from 0 to i
// having the jth bit as set
int[,] P = new int[N + 1, MAX_BITS + 1];
for(int i = 0; i <= MAX_BITS; i++)
{
P[0, i] = 0;
}
for(int i = 0; i < N; i++)
{
for(int j = 1; j <= MAX_BITS; j++)
{
// prefix sum from 0 to i for each bit
// position jhas the value of sum from 0
// to i-1 for each j
if (i != 0)
{
P[i, j] = P[i - 1, j];
}
// If jth bit set then increment P[i][j] by 1
bool isJthBitSet = isKthBitSet(arr[i], j);
if (isJthBitSet)
{
P[i, j]++;
}
}
}
return P;
}
// Returns the answer for each query with range
// L to R querying for the number of elements with
// the Kth bit set in the range
static int answerQuery(int L, int R, int K, int[,] P)
{
// Number of elements in range L to R with Kth
// bit set = (Number of elements from 0 to R with
// kth bit set) - (Number of elements from 0 to L-1
// with kth bit set)
if (L != 0)
{
return P[R, K] - P[L - 1, K];
}
else
{
return P[R, K];
}
}
// Print the answer for all queries
static void answerQueries(int[,] queries, int Q,
int[] arr, int N)
{
// Build Prefix Array to answer queries efficiently
int[,] P = buildPrefixArray(N, arr);
int query_L, query_R, query_K;
for(int i = 0; i < Q; i++)
{
query_L = queries[i, 0] - 1;
query_R = queries[i, 1] - 1;
query_K = queries[i, 2];
Console.WriteLine("Result for Query " +
(i + 1) + " = " +
answerQuery(query_L,
query_R,
query_K, P));
}
}
// Driver Code
static public void Main()
{
int[] arr = { 8, 9, 1, 3 };
int N = arr.Length;
/* queries[][] denotes the array of queries
where each query has three integers
query[i][0] -> Value of L for ith query
query[i][0] -> Value of R for ith query
query[i][0] -> Value of K for ith query */
int[,] queries = { { 1, 3, 4 }, { 2, 4, 1 } };
int Q = queries.GetLength(0);
answerQueries(queries, Q, arr, N);
}
}
// This code is contributed by rag2127
JavaScript
<script>
/* Javascript Program to find the number of elements
in a range L to R having the Kth bit as set */
// Maximum bits required in binary representation
// of an array element
let MAX_BITS = 32;
/* Returns true if n has its kth bit as set,
else returns false */
function isKthBitSet(n,k)
{
if((n & (1 << (k - 1))) != 0)
{
return true;
}
return false;
}
// Return pointer to the prefix sum array
function buildPrefixArray(N,arr)
{
// Build a prefix sum array P[][]
// where P[i][j] represents the number of
// elements from 0 to i having the jth bit as set
let P = new Array(N + 1);
for(let i=0;i<P.length;i++)
{
P[i]=new Array(MAX_BITS+1);
}
for(let i = 0; i <= MAX_BITS; i++)
{
P[0][i] = 0;
}
for(let i = 0; i < N; i++)
{
for(let j = 1; j <= MAX_BITS; j++)
{
// prefix sum from 0 to i for each bit
// position jhas the value of sum from 0
// to i-1 for each j
if(i != 0)
{
P[i][j] = P[i - 1][j];
}
// if jth bit set then increment P[i][j] by 1
let isJthBitSet = isKthBitSet(arr[i], j);
if(isJthBitSet)
{
P[i][j]++;
}
}
}
return P;
}
/* Returns the answer for each query with range
L to R querying for the number of elements with
the Kth bit set in the range */
function answerQuery(L,R,K,P)
{
/* Number of elements in range L to R with Kth
bit set = (Number of elements from 0 to R with
kth bit set) - (Number of elements from 0 to L-1
with kth bit set) */
if(L != 0)
{
return P[R][K] - P[L - 1][K];
}
else
{
return P[R][K];
}
}
// Print the answer for all queries
function answerQueries(queries,Q,arr,N)
{
// Build Prefix Array to answer queries efficiently
let P = buildPrefixArray(N, arr);
let query_L, query_R, query_K;
for(let i = 0; i < Q; i++)
{
query_L = queries[i][0] - 1;
query_R = queries[i][1] - 1;
query_K = queries[i][2];
document.write("Result for Query " + (i + 1) + " = " + answerQuery(query_L, query_R, query_K, P)+"<br>");
}
}
// Driver Code
let arr=[8, 9, 1, 3];
let N = arr.length;
/* queries[][] denotes the array of queries
where each query has three integers
query[i][0] -> Value of L for ith query
query[i][0] -> Value of R for ith query
query[i][0] -> Value of K for ith query */
let queries = [[1, 3, 4],[2, 4, 1]];
let Q = queries.length;
answerQueries(queries, Q, arr, N);
// This code is contributed by patel2127
</script>
Output: Result for Query 1 = 2
Result for Query 2 = 3
Time Complexity of building the Prefix array is O(N * Maximum number of Bits) and each query is answered in O(1).
Auxiliary space : O(N * Maximum Number of Bits) is required to build the Prefix Sum Array
Similar Reads
Queries to count array elements from a given range having a single set bit Given an array arr[] consisting of N integers and a 2D array Q[][] consisting of queries of the following two types: 1 L R: Print the count of numbers from the range [L, R] having only a single set bit.2 X V: Update the array element at Xth index with V.Examples: Input: arr[] = { 12, 11, 16, 2, 32 }
15+ min read
Queries to count array elements from a given range having a single set bit Given an array arr[] consisting of positive integers and an array Q[][] consisting of queries, the task for every ith query is to count array elements from the range [Q[i][0], Q[i][1]] with only one set bit. Examples: Input: arr[] = {12, 11, 16, 8, 2, 5, 1, 3, 256, 1}, queries[][] = {{0, 9}, {4, 9}}
7 min read
Count of pairs in an Array with same number of set bits Given an array arr containing N integers, the task is to count the possible number of pairs of elements with the same number of set bits. Examples: Input: N = 8, arr[] = {1, 2, 3, 4, 5, 6, 7, 8} Output: 9 Explanation: Elements with 1 set bit: 1, 2, 4, 8 Elements with 2 set bits: 3, 5, 6 Elements wit
7 min read
Queries for bitwise AND in the index range [L, R] of the given array Given an array arr[] of N and Q queries consisting of a range [L, R]. the task is to find the bit-wise AND of all the elements of in that index range.Examples: Input: arr[] = {1, 3, 1, 2, 3, 4}, q[] = {{0, 1}, {3, 5}} Output: 1 0 1 AND 3 = 1 2 AND 3 AND 4 = 0Input: arr[] = {1, 2, 3, 4, 5}, q[] = {{0
8 min read
Queries for number of distinct elements in a subarray Given a array 'a[]' of size n and number of queries q. Each query can be represented by two integers l and r. Your task is to print the number of distinct integers in the subarray l to r. Given a[i] <= 106 Examples: Input : a[] = {1, 1, 2, 1, 3} q = 3 0 4 1 3 2 4 Output :3 2 3 In query 1, number
10 min read
Count set bits in index range [L, R] in given Array for Q queries Given an array arr[] containing N integers and an array queries[] containing Q queries in the form of {L, R}, the task is to count the total number of set bits from L to R in array arr for each query. Example: Input: arr[]={1, 2, 3, 4, 5, 6}, queries[]={{0, 2}, {1, 1}, {3, 5}}Output:425Explanation:Q
6 min read
Queries for bitwise OR in the index range [L, R] of the given array Given an array arr[] of N and Q queries consisting of a range [L, R]. the task is to find the bit-wise OR of all the elements of in that index range.Examples: Input: arr[] = {1, 3, 1, 2, 3, 4}, q[] = {{0, 1}, {3, 5}} Output: 3 7 1 OR 3 = 3 2 OR 3 OR 4 = 7Input: arr[] = {1, 2, 3, 4, 5}, q[] = {{0, 4}
9 min read
Program to count number of set bits in an (big) array Given an integer array of length N (an arbitrarily large number). How to count number of set bits in the array?The simple approach would be, create an efficient method to count set bits in a word (most prominent size, usually equal to bit length of processor), and add bits from individual elements o
12 min read
Sorting array elements with set bits equal to K Given an array of integers and a number K . The task is to sort only those elements of the array whose total set bits are equal to K. Sorting must be done at their relative positions only without affecting any other elements.Examples: Input : arr[] = {32, 1, 9, 4, 64, 2}, K = 1 Output : 1 2 9 4 32 6
6 min read
Find the number of elements X such that X + K also exists in the array Given an array a[] and an integer k, find the number of elements x in this array such that the sum of x and k is also present in the array. Examples: Input: { 3, 6, 2, 8, 7, 6, 5, 9 } and k = 2Output: 5 Explanation:Elements {3, 6, 7, 6, 5} in this array have x + 2 value that is{5, 8, 9, 8, 7} presen
10 min read