给定一个由N 个正数和形式为[L, R] 的Q查询组成的数组arr[] ,任务是为每个查询找到子数组 [L, R] 中为 2 的幂的元素数量。
例子:
Input: arr[] = { 3, 8, 5, 2, 5, 10 }, Q = {{0, 4}, {3, 5}}
Output:
2
1
Explanation:
For Query 1, the subarray [3, 8, 5, 2, 5] has 2 elements which are a power of two, 8 and 2.
For Query 2, the subarray {2, 5, 10} has 1 element which are a power of two, 2.
Input: arr[] = { 1, 2, 3, 4, 5, 6 }, Q = {{0, 4}, {1, 5}}
Output:
3
2
朴素的方法:为了解决上面提到的问题,朴素的方法是对于所有的 Q 查询,我们可以遍历数组中的每个 L 和 R,并找到子数组 [L, R ]。
时间复杂度: O(N * Q)
有效的方法:
为了优化上述方法,这里的想法是使用前缀和数组。
- 最初,前缀和数组包含所有索引的 0。
- 如果当前数组元素是 2 的幂,则遍历给定数组并将此索引的前缀数组设置为 1,否则保留为 0。
- 现在,通过添加前一个索引前缀数组值来计算当前索引的前缀和来获得前缀和。 prefix[i] 将存储从 1 到 i 的 2 的幂的元素数量。
- 一旦我们有了前缀数组,我们只需要为每个查询返回prefix[r] – prefix[l-1] 。
下面是上述方法的实现,
C++
// C++ implementation to find
// elements that are a power of two
#include
using namespace std;
const int MAX = 10000;
// prefix[i] is going to store the
// number of elements which are a
// power of two till i (including i).
int prefix[MAX + 1];
bool isPowerOfTwo(int x)
{
if (x && (!(x & (x - 1))))
return true;
return false;
}
// Function to find the maximum range
// whose sum is divisible by M.
void computePrefix(int n, int a[])
{
// Calculate the prefix sum
if (isPowerOfTwo(a[0]))
prefix[0] = 1;
for (int i = 1; i < n; i++) {
prefix[i] = prefix[i - 1];
if (isPowerOfTwo(a[i]))
prefix[i]++;
}
}
// Function to return the number of elements
// which are a power of two in a subarray
int query(int L, int R)
{
return prefix[R] - prefix[L - 1];
}
// Driver code
int main()
{
int A[] = { 3, 8, 5, 2, 5, 10 };
int N = sizeof(A) / sizeof(A[0]);
int Q = 2;
computePrefix(N, A);
cout << query(0, 4) << "\n";
cout << query(3, 5) << "\n";
return 0;
}
Java
// Java implementation to find
// elements that are a power of two
import java.util.*;
class GFG{
static final int MAX = 10000;
// prefix[i] is going to store the
// number of elements which are a
// power of two till i (including i).
static int[] prefix = new int[MAX + 1];
static boolean isPowerOfTwo(int x)
{
if (x != 0 && ((x & (x - 1)) == 0))
return true;
return false;
}
// Function to find the maximum range
// whose sum is divisible by M.
static void computePrefix(int n, int a[])
{
// Calculate the prefix sum
if (isPowerOfTwo(a[0]))
prefix[0] = 1;
for (int i = 1; i < n; i++)
{
prefix[i] = prefix[i - 1];
if (isPowerOfTwo(a[i]))
prefix[i]++;
}
}
// Function to return the number of elements
// which are a power of two in a subarray
static int query(int L, int R)
{
if (L == 0)
return prefix[R];
return prefix[R] - prefix[L - 1];
}
// Driver code
public static void main(String[] args)
{
int A[] = { 3, 8, 5, 2, 5, 10 };
int N = A.length;
int Q = 2;
computePrefix(N, A);
System.out.println(query(0, 4));
System.out.println(query(3, 5));
}
}
// This code is contributed by offbeat
Python3
# Python3 implementation to find
# elements that are a power of two
MAX = 10000
# prefix[i] is going to store the
# number of elements which are a
# power of two till i (including i).
prefix = [0] * (MAX + 1)
def isPowerOfTwo(x):
if (x and (not (x & (x - 1)))):
return True
return False
# Function to find the maximum range
# whose sum is divisible by M.
def computePrefix(n, a):
# Calculate the prefix sum
if (isPowerOfTwo(a[0])):
prefix[0] = 1
for i in range(1, n):
prefix[i] = prefix[i - 1]
if (isPowerOfTwo(a[i])):
prefix[i] += 1
# Function to return the number of elements
# which are a power of two in a subarray
def query(L, R):
return prefix[R] - prefix[L - 1]
# Driver code
if __name__ == "__main__":
A = [ 3, 8, 5, 2, 5, 10 ]
N = len(A)
Q = 2
computePrefix(N, A)
print(query(0, 4))
print(query(3, 5))
# This code is contributed by chitranayal
C#
// C# implementation to find
// elements that are a power of two
using System;
class GFG{
static int MAX = 10000;
// prefix[i] is going to store the
// number of elements which are a
// power of two till i (including i).
static int[] prefix = new int[MAX + 1];
static bool isPowerOfTwo(int x)
{
if (x != 0 && ((x & (x - 1)) == 0))
return true;
return false;
}
// Function to find the maximum range
// whose sum is divisible by M.
static void computePrefix(int n, int []a)
{
// Calculate the prefix sum
if (isPowerOfTwo(a[0]))
prefix[0] = 1;
for (int i = 1; i < n; i++)
{
prefix[i] = prefix[i - 1];
if (isPowerOfTwo(a[i]))
prefix[i]++;
}
}
// Function to return the number of elements
// which are a power of two in a subarray
static int query(int L, int R)
{
if (L == 0)
return prefix[R];
return prefix[R] - prefix[L - 1];
}
// Driver code
public static void Main()
{
int []A = { 3, 8, 5, 2, 5, 10 };
int N = A.Length;
computePrefix(N, A);
Console.WriteLine(query(0, 4));
Console.WriteLine(query(3, 5));
}
}
// This code is contributed by Code_Mech
Javascript
输出:
2
1
时间复杂度: O(max(Q, N))
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live