# 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 = 7

Input: arr[] = {1, 2, 3, 4, 5}, q[] = {{0, 4}, {1, 3}}
Output:
7
7

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive approach: Iterate through the range and find bit-wise OR of all the numbers in that range. This will take O(n) time for each query.

Efficient approach: If we look at the integers as binary number, we can easily see that condition for ith bit of our answer to be set is that ith bit of any of the integers in the range [L, R] should be set.
So, we will calculate prefix-count for each bit. We will use this to find the number of integers in the range with ith bit set. If its greater than 0 then the ith bit of our answer will also be set.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `#define MAX 100000 ` `#define bitscount 32 ` `using` `namespace` `std; ` ` `  `// Array to store bit-wise ` `// prefix count ` `int` `prefix_count[bitscount][MAX]; ` ` `  `// Function to find the prefix sum ` `void` `findPrefixCount(``int` `arr[], ``int` `n) ` `{ ` ` `  `    ``// Loop for each bit ` `    ``for` `(``int` `i = 0; i < bitscount; i++) { ` `        ``// Loop to find prefix count ` `        ``prefix_count[i] = ((arr >> i) & 1); ` `        ``for` `(``int` `j = 1; j < n; j++) { ` `            ``prefix_count[i][j] = ((arr[j] >> i) & 1); ` `            ``prefix_count[i][j] += prefix_count[i][j - 1]; ` `        ``} ` `    ``} ` `} ` ` `  `// Function to answer query ` `int` `rangeOr(``int` `l, ``int` `r) ` `{ ` ` `  `    ``// To store the answer ` `    ``int` `ans = 0; ` ` `  `    ``// Loop for each bit ` `    ``for` `(``int` `i = 0; i < bitscount; i++) { ` `        ``// To store the number of variables ` `        ``// with ith bit set ` `        ``int` `x; ` `        ``if` `(l == 0) ` `            ``x = prefix_count[i][r]; ` `        ``else` `            ``x = prefix_count[i][r] ` `                ``- prefix_count[i][l - 1]; ` ` `  `        ``// Condition for ith bit ` `        ``// of answer to be set ` `        ``if` `(x != 0) ` `            ``ans = (ans | (1 << i)); ` `    ``} ` ` `  `    ``return` `ans; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 7, 5, 3, 5, 2, 3 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(``int``); ` ` `  `    ``findPrefixCount(arr, n); ` ` `  `    ``int` `queries[] = { { 1, 3 }, { 4, 5 } }; ` `    ``int` `q = ``sizeof``(queries) / ``sizeof``(queries); ` ` `  `    ``for` `(``int` `i = 0; i < q; i++) ` `        ``cout << rangeOr(queries[i], ` `                        ``queries[i]) ` `             ``<< endl; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach  ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `     `  `static` `int` `MAX = ``100000``; ` `static` `int` `bitscount = ``32``; ` ` `  `// Array to store bit-wise ` `// prefix count ` `static` `int` `[][]prefix_count = ``new` `int` `[bitscount][MAX]; ` ` `  `// Function to find the prefix sum ` `static` `void` `findPrefixCount(``int` `arr[], ``int` `n) ` `{ ` ` `  `    ``// Loop for each bit ` `    ``for` `(``int` `i = ``0``; i < bitscount; i++) ` `    ``{ ` `        ``// Loop to find prefix count ` `        ``prefix_count[i][``0``] = ((arr[``0``] >> i) & ``1``); ` `        ``for` `(``int` `j = ``1``; j < n; j++)  ` `        ``{ ` `            ``prefix_count[i][j] = ((arr[j] >> i) & ``1``); ` `            ``prefix_count[i][j] += prefix_count[i][j - ``1``]; ` `        ``} ` `    ``} ` `} ` ` `  `// Function to answer query ` `static` `int` `rangeOr(``int` `l, ``int` `r) ` `{ ` ` `  `    ``// To store the answer ` `    ``int` `ans = ``0``; ` ` `  `    ``// Loop for each bit ` `    ``for` `(``int` `i = ``0``; i < bitscount; i++) ` `    ``{ ` `        ``// To store the number of variables ` `        ``// with ith bit set ` `        ``int` `x; ` `        ``if` `(l == ``0``) ` `            ``x = prefix_count[i][r]; ` `        ``else` `            ``x = prefix_count[i][r] ` `                ``- prefix_count[i][l - ``1``]; ` ` `  `        ``// Condition for ith bit ` `        ``// of answer to be set ` `        ``if` `(x != ``0``) ` `            ``ans = (ans | (``1` `<< i)); ` `    ``} ` ` `  `    ``return` `ans; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main (String[] args)  ` `{ ` `     `  `    ``int` `arr[] = { ``7``, ``5``, ``3``, ``5``, ``2``, ``3` `}; ` `    ``int` `n = arr.length; ` `    ``findPrefixCount(arr, n); ` `    ``int` `queries[][] = { { ``1``, ``3` `}, { ``4``, ``5` `} }; ` `    ``int` `q = queries.length; ` `    ``for` `(``int` `i = ``0``; i < q; i++) ` `            ``System.out.println (rangeOr(queries[i][``0``],queries[i][``1``])); ` ` `  `} ` `} ` ` `  `// This code is contributed by Tushil.  `

## Python3

 `# Python3 implementation of the approach  ` ` `  `import` `numpy as np ` ` `  `MAX` `=` `100000` `bitscount ``=` `32` ` `  `# Array to store bit-wise  ` `# prefix count  ` `prefix_count ``=` `np.zeros((bitscount,``MAX``));  ` ` `  `# Function to find the prefix sum  ` `def` `findPrefixCount(arr, n) : ` ` `  `    ``# Loop for each bit  ` `    ``for` `i ``in` `range``(``0``, bitscount) : ` `        ``# Loop to find prefix count  ` `        ``prefix_count[i][``0``] ``=` `((arr[``0``] >> i) & ``1``);  ` `         `  `        ``for` `j ``in` `range``(``1``, n) : ` `            ``prefix_count[i][j] ``=` `((arr[j] >> i) & ``1``);  ` `            ``prefix_count[i][j] ``+``=` `prefix_count[i][j ``-` `1``];  ` ` `  `# Function to answer query  ` `def` `rangeOr(l, r) :  ` ` `  `    ``# To store the answer  ` `    ``ans ``=` `0``;  ` ` `  `    ``# Loop for each bit  ` `    ``for` `i ``in` `range``(bitscount) : ` `         `  `        ``# To store the number of variables  ` `        ``# with ith bit set  ` `        ``x ``=` `0``;  ` `         `  `        ``if` `(l ``=``=` `0``) : ` `            ``x ``=` `prefix_count[i][r];  ` `        ``else` `: ` `            ``x ``=` `prefix_count[i][r] ``-` `prefix_count[i][l ``-` `1``];  ` ` `  `        ``# Condition for ith bit  ` `        ``# of answer to be set  ` `        ``if` `(x !``=` `0``) : ` `            ``ans ``=` `(ans | (``1` `<< i));  ` `             `  `    ``return` `ans;  ` ` `  ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``arr ``=` `[ ``7``, ``5``, ``3``, ``5``, ``2``, ``3` `];  ` `    ``n ``=` `len``(arr); ` ` `  `    ``findPrefixCount(arr, n);  ` ` `  `    ``queries ``=` `[ [ ``1``, ``3` `], [ ``4``, ``5` `] ];  ` `     `  `    ``q ``=` `len``(queries);  ` ` `  `    ``for` `i ``in` `range``(q) : ` `        ``print``(rangeOr(queries[i][``0``], queries[i][``1``])); ` ` `  `# This code is contributed by AnkitRai01 `

\

## C#

 `// C# implementation of the approach  ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `     `  `static` `int` `MAX = 100000; ` `static` `int` `bitscount = 32; ` ` `  `// Array to store bit-wise ` `// prefix count ` `static` `int` `[,]prefix_count = ``new` `int` `[bitscount,MAX]; ` ` `  `// Function to find the prefix sum ` `static` `void` `findPrefixCount(``int` `[]arr, ``int` `n) ` `{ ` ` `  `    ``// Loop for each bit ` `    ``for` `(``int` `i = 0; i < bitscount; i++) ` `    ``{ ` `        ``// Loop to find prefix count ` `        ``prefix_count[i,0] = ((arr >> i) & 1); ` `        ``for` `(``int` `j = 1; j < n; j++)  ` `        ``{ ` `            ``prefix_count[i,j] = ((arr[j] >> i) & 1); ` `            ``prefix_count[i,j] += prefix_count[i,j - 1]; ` `        ``} ` `    ``} ` `} ` ` `  `// Function to answer query ` `static` `int` `rangeOr(``int` `l, ``int` `r) ` `{ ` ` `  `    ``// To store the answer ` `    ``int` `ans = 0; ` ` `  `    ``// Loop for each bit ` `    ``for` `(``int` `i = 0; i < bitscount; i++) ` `    ``{ ` `        ``// To store the number of variables ` `        ``// with ith bit set ` `        ``int` `x; ` `        ``if` `(l == 0) ` `            ``x = prefix_count[i,r]; ` `        ``else` `            ``x = prefix_count[i,r] ` `                ``- prefix_count[i,l - 1]; ` ` `  `        ``// Condition for ith bit ` `        ``// of answer to be set ` `        ``if` `(x != 0) ` `            ``ans = (ans | (1 << i)); ` `    ``} ` ` `  `    ``return` `ans; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main (String[] args)  ` `{ ` `     `  `    ``int` `[]arr = { 7, 5, 3, 5, 2, 3 }; ` `    ``int` `n = arr.Length; ` `    ``findPrefixCount(arr, n); ` `    ``int` `[,]queries = { { 1, 3 }, { 4, 5 } }; ` `    ``int` `q = queries.GetLength(0); ` `    ``for` `(``int` `i = 0; i < q; i++) ` `            ``Console.WriteLine(rangeOr(queries[i,0],queries[i,1])); ` ` `  `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## PHP

 `> ``\$i``) & 1); ` `        ``for` `(``\$j` `= 1; ``\$j` `< ``\$n``; ``\$j``++) ` `        ``{ ` `            ``\$prefix_count``[``\$i``][``\$j``] = ((``\$arr``[``\$j``] >> ``\$i``) & 1); ` `            ``\$prefix_count``[``\$i``][``\$j``] += ``\$prefix_count``[``\$i``][``\$j` `- 1]; ` `        ``} ` `    ``} ` `} ` ` `  `// Function to answer query ` `function` `rangeOr(``\$l``, ``\$r``) ` `{ ` `    ``global` `\$MAX``,``\$bitscount``,``\$prefix_count``; ` `     `  `    ``// To store the answer ` `    ``\$ans` `= 0; ` ` `  `    ``// Loop for each bit ` `    ``for` `(``\$i` `= 0; ``\$i` `< ``\$bitscount``; ``\$i``++)  ` `    ``{ ` `        ``// To store the number of variables ` `        ``// with ith bit set ` `         `  `        ``if` `(``\$l` `== 0) ` `            ``\$x` `= ``\$prefix_count``[``\$i``][``\$r``]; ` `        ``else` `            ``\$x` `= ``\$prefix_count``[``\$i``][``\$r``] ` `                ``- ``\$prefix_count``[``\$i``][l - 1]; ` ` `  `        ``// Condition for ith bit ` `        ``// of answer to be set ` `        ``if` `(``\$x` `!= 0) ` `            ``\$ans` `= (``\$ans` `| (1 << ``\$i``)); ` `    ``} ` ` `  `    ``return` `\$ans``; ` `} ` ` `  `    ``// Driver code ` `    ``\$arr` `=``array``( 7, 5, 3, 5, 2, 3 ); ` `    ``\$n` `= sizeof(``\$arr``) / sizeof(``\$arr``); ` ` `  `    ``findPrefixCount(``\$arr``, ``\$n``); ` ` `  `    ``\$queries` `= ``array``(``array``( 1, 3 ), ``array``( 4, 5 )); ` `    ``\$q` `= sizeof(``\$queries``) / sizeof(``\$queries``); ` ` `  `    ``for` `(``\$i` `= 0; ``\$i` `< ``\$q``; ``\$i``++) ` `        ``echo` `rangeOr(``\$queries``[``\$i``], ` `                        ``\$queries``[``\$i``]).``"\n"``; ` ` `  `    ``return` `0; ` `     `  `    ``// This code is contributed by ChitraNayal ` `?> `

Output:

```7
3
```

Time complexity for pre-computation is O(n) and each query can be answered in O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
{"ft_lang_label":"__label__en","ft_lang_prob":0.57170856,"math_prob":0.9824279,"size":9619,"snap":"2020-34-2020-40","text_gpt3_token_len":3220,"char_repetition_ratio":0.15715028,"word_repetition_ratio":0.34590247,"special_character_ratio":0.37706622,"punctuation_ratio":0.15368532,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99984264,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-27T07:25:00Z\",\"WARC-Record-ID\":\"<urn:uuid:6463f851-5111-4fb9-ac65-1e3d969e4097>\",\"Content-Length\":\"171741\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:1f40a0c4-0647-4713-bc23-4f6b1550a4c2>\",\"WARC-Concurrent-To\":\"<urn:uuid:871c32d7-dd09-41c8-b48e-4c436e4def99>\",\"WARC-IP-Address\":\"204.237.143.56\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/queries-for-bitwise-or-in-the-index-range-l-r-of-the-given-array/\",\"WARC-Payload-Digest\":\"sha1:4BOA36URYKDU7DLKOLN5Z473MTG4UXPW\",\"WARC-Block-Digest\":\"sha1:H4U4ANM7BNSUTVENOFWWCZ4UGUUWLJNV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600400265461.58_warc_CC-MAIN-20200927054550-20200927084550-00325.warc.gz\"}"}