using
System;
class
GFG
{
static
int
MAX = 100000;
static
int
bitscount =32;
static
int
[,]prefix_count =
new
int
[bitscount,MAX];
static
void
findPrefixCount(
int
[]arr,
int
n)
{
for
(
int
i = 0; i < bitscount; i++)
{
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];
}
}
}
static
int
rangeAnd(
int
l,
int
r)
{
int
ans = 0;
for
(
int
i = 0; i < bitscount; i++)
{
int
x;
if
(l == 0)
x = prefix_count[i,r];
else
x = prefix_count[i,r]
- prefix_count[i,l - 1];
if
(x == r - l + 1)
ans = (ans | (1 << i));
}
return
ans;
}
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(rangeAnd(queries[i,0],queries[i,1]));
}
}