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 for count of array elements with values in given range with updates
Given an array arr[] of size N and a matrix Q consisting of queries of the following two types:Â 1 L R : Print the number of elements lying in the range [L, R].2 i x : Set arr[i] = x Examples:Â Input: arr[] = {1, 2, 2, 3, 4, 4, 5, 6}, Q = {{1, {3, 5}}, {1, {2, 4}}, {1, {1, 2}}, {2, {1, 7}}, {1, {1,
15+ min read
Range Queries to Find number of sub-arrays with a given xor
Given an array arr[] of size n and q queries and an integer k. Each query consists of an index range [l, r] and the task is to count the number of pairs of indices i and j such that l ≤ i ≤ j ≤ r (1-based indexing) and the xor of the elements a[i], a[i + 1], ..., a[j] is equal to k. Example
5 min read
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
Queries for number of distinct elements in a subarray | Set 2
Given an array arr[] of N integers and Q queries. Each query can be represented by two integers L and R. The task is to find the count of distinct integers in the subarray arr[L] to arr[R].Examples: Input: arr[] = {1, 1, 3, 3, 5, 5, 7, 7, 9, 9 }, L = 0, R = 4 Output: 3Input: arr[] = { 1, 1, 2, 1, 3
12 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
Array range queries for elements with frequency same as value
Given an array of N numbers, the task is to answer Q queries of the following type: query(start, end) = Number of times a number x occurs exactly x times in a subarray from start to end Examples: Input : arr = {1, 2, 2, 3, 3, 3} Query 1: start = 0, end = 1, Query 2: start = 1, end = 1, Query 3: star
15+ min read
Maximum number of elements from an array B[] that are present in ranges [A[i] + K, A[i] - K]
Given two arrays A[] of size N and B[] of size M and an integer K, the task is to select at most one element from array B[] for every element A[i] such that the element lies in the range [A[i] - K, A[i] + K] ( for 0 <= i <= N - 1 ). Print the maximum number of elements that can be selected fro
7 min read