查找最大索引,直到元素的按位与对于 Q 查询至少为 X
给定大小为N和Q的整数数组arr[]和queries[] ,任务是找到每个查询Q[i]的最大索引,使得从开始到该索引的每个元素的按位与至少为 Q[i ],即 (arr[1] & arr[2] &. . .& arr[k]) ≥ Q[i]。
示例:
Input: arr[ ] = [3, 7, 9, 16] , queries[] = [ 2, 1 ]
Output: 2 3
Explanation: Answer for the first query is 2. Since, (3 & 7) = 3 >= 2. So largest index is 2.
Answer for the second query is 3. Since, (3 & 7 & 9) = 1 >= 1. So largest index is 3.
Input: arr[ ] = [1, 2, 3], queries[ ] = [10]
Output: -1
Explanation: Since the query 10 is large then none of the bitwise And subarray from 1 to index is possible,
So answer is -1.
朴素方法:该方法的基本思想是遍历数组arr[] 为每个查询并找到满足条件的最大索引。
请按照以下步骤解决问题:
- 初始化一个空数组answer以存储查询的答案。
- 从0 迭代到 Q (假设迭代器是i )。
- 声明一个名为bit_and的变量并用 arr[0] 对其进行初始化。
- 如果 arr[0] 小于X ,则将 0 添加到答案并继续
- 声明一个变量count并将其初始化为 1 以存储每个查询的答案。
- 从 1 迭代到 arr 的长度(假设迭代器是 j)。
- 用arr[j] & bit_and 更新 bit_and 。
- 如果bit_and大于等于X ,则将count增加 1 并继续。
- 否则,断。
- 将计数添加到答案中。
- 返回答案。
以下是上述方法的实现:
C++
// C++ code to implement the approach
#include
using namespace std;
// Function to find the largest index
vector bitwiseAnd(int n, int q,
vector& arr,
vector& queries)
{
vector answer;
for (int i = 0; i < q; i++) {
int x = queries[i];
int bit_and = arr[0];
if (arr[0] < x) {
answer.push_back(0);
continue;
}
int count = 1;
// Checking for the largest index
for (int j = 1; j < n; j++) {
bit_and = bit_and & arr[j];
if (bit_and >= x) {
count++;
continue;
}
else {
break;
}
}
answer.push_back(count);
}
return answer;
}
//Driver code
int main()
{
int N = 4, Q = 2;
vector arr = { 3, 7, 9, 16 };
vector queries = { 2, 1 };
// Function call
vector ans
= bitwiseAnd(N, Q, arr, queries);
for (auto& i : ans)
cout << i << " ";
return 0;
}
Python3
# Python code for the above approach
# Function to find the largest index
def bitwiseAnd(n, q, arr,queries):
answer =[]
for i in range(q):
x = queries[i]
bit_and = arr[0]
if (arr[0] < x) :
answer.append(0)
continue
count = 1
# Checking for the largest index
for j in range(1,n):
bit_and = bit_and & arr[j]
if (bit_and >= x):
count += 1
continue
else :
break
answer.append(count)
return answer
# Driver code
N,Q = 4,2
arr = [3, 7, 9, 16]
queries = [2, 1]
# Function call
ans = bitwiseAnd(N, Q, arr, queries)
for i in ans :
print(i,end=" ")
# This code is contributed by shinjanpatra
Javascript
C++
// C++ code to implement the approach
#include
using namespace std;
// Function to find the largest index
vector bitwiseAnd(int n, int q,
vector& arr,
vector& queries)
{
vector answer;
vector prefix(n, 0);
prefix[0] = arr[0];
// Constructing the prefix
// bitwise and array.
for (int i = 1; i < n; i++) {
prefix[i] = prefix[i - 1] & arr[i];
}
for (int i = 0; i < q; i++) {
int x = queries[i];
int st = 0;
int end = n - 1;
int count = 0;
// Binary Searching the largest index
while (st <= end) {
int mid = (st + end) / 2;
if (prefix[mid] >= x) {
count = mid + 1;
st = mid + 1;
}
else {
end = mid - 1;
}
}
answer.push_back(count);
}
return answer;
}
// Driver code
int main()
{
int N = 4, Q = 2;
vector arr = { 3, 7, 9, 16 };
vector queries = { 2, 1 };
// Function call
vector ans
= bitwiseAnd(N, Q, arr, queries);
for (auto& i : ans)
cout << i << " ";
return 0;
}
Javascript
Python3
# Python code to implement the approach
# Function to find the largest index
def bitwiseAnd (n, q, arr, queries):
answer = []
prefix = [0]*n
prefix[0] = arr[0]
# Constructing the prefix
# bitwise and array.
for i in range(1,n):
prefix[i] = prefix[i - 1] & arr[i]
for i in range(q):
x = queries[i]
st = 0
end = n - 1
count = 0
# Binary Searching the largest index
while (st <= end):
mid = (st + end) // 2
if (prefix[mid] >= x):
count = mid + 1
st = mid + 1
else:
end = mid - 1
answer.append(count)
return answer
# Driver code
N,Q = 4,2
arr = [3, 7, 9, 16]
queries = [2, 1]
# Function call
ans = bitwiseAnd(N, Q, arr, queries)
for i in ans:
print(i,end=" ")
# This code is contributed by shinjanpatra
2 3
时间复杂度:O(N * Q)
辅助空间:O(1)
有效方法:有效方法的想法基于以下观察:
The bitwise AND operation when applied from start of array to ith index is monotonically decreasing for an array. So use pre AND operation on the array and then use binary search to find the largest index having a given value.
请按照以下步骤解决问题:
- 声明一个名为answer的空数组来存储查询的答案。
- 声明一个大小为N的名为 prefix 的数组来存储数组的前缀 AND 直到第 i 个索引。
- 用 arr[0] 更新前缀 [0]。
- 从 1 迭代到N (假设迭代器是 j)。
- 用arr[j] & prefix[j-1]更新prefix[j ] 。
- 从0 迭代到 Q (假设迭代器是 i)。
- 声明 2 个名为st和end的变量,并用0 和 N – 1对其进行初始化。分别。
- 声明一个名为count的变量并将其初始化为 0。
- 启动条件st小于等于END的 while 循环。
- 声明一个名为mid的变量并使用 ( st + end ) / 2 对其进行初始化。
- 如果prefix[mid]大于等于queries[mid] ,更新count为mid+1 , st为mid+1 。
- 否则,将end更新为mid-1 。
- 将计数添加到答案中。
- 返回答案。
下面是上述方法的实现。
C++
// C++ code to implement the approach
#include
using namespace std;
// Function to find the largest index
vector bitwiseAnd(int n, int q,
vector& arr,
vector& queries)
{
vector answer;
vector prefix(n, 0);
prefix[0] = arr[0];
// Constructing the prefix
// bitwise and array.
for (int i = 1; i < n; i++) {
prefix[i] = prefix[i - 1] & arr[i];
}
for (int i = 0; i < q; i++) {
int x = queries[i];
int st = 0;
int end = n - 1;
int count = 0;
// Binary Searching the largest index
while (st <= end) {
int mid = (st + end) / 2;
if (prefix[mid] >= x) {
count = mid + 1;
st = mid + 1;
}
else {
end = mid - 1;
}
}
answer.push_back(count);
}
return answer;
}
// Driver code
int main()
{
int N = 4, Q = 2;
vector arr = { 3, 7, 9, 16 };
vector queries = { 2, 1 };
// Function call
vector ans
= bitwiseAnd(N, Q, arr, queries);
for (auto& i : ans)
cout << i << " ";
return 0;
}
Javascript
Python3
# Python code to implement the approach
# Function to find the largest index
def bitwiseAnd (n, q, arr, queries):
answer = []
prefix = [0]*n
prefix[0] = arr[0]
# Constructing the prefix
# bitwise and array.
for i in range(1,n):
prefix[i] = prefix[i - 1] & arr[i]
for i in range(q):
x = queries[i]
st = 0
end = n - 1
count = 0
# Binary Searching the largest index
while (st <= end):
mid = (st + end) // 2
if (prefix[mid] >= x):
count = mid + 1
st = mid + 1
else:
end = mid - 1
answer.append(count)
return answer
# Driver code
N,Q = 4,2
arr = [3, 7, 9, 16]
queries = [2, 1]
# Function call
ans = bitwiseAnd(N, Q, arr, queries)
for i in ans:
print(i,end=" ")
# This code is contributed by shinjanpatra
2 3
时间复杂度:O(N+ Q* log N)
辅助空间:O(N)