📌  相关文章
📜  Q查询的给定范围子数组中为2的幂的元素的计数

📅  最后修改于: 2021-05-08 19:00:18             🧑  作者: Mango

给定一个数组nrr [] ,该数组arr []N个正数和形式为[L,R]的Q个查询组成,任务是在每个查询的子数组[L,R]中查找为2的幂的元素数。

例子:

朴素的方法:为解决上述问题,朴素的方法是对于所有Q查询,我们可以遍历数组中的每个L和R,并找到子数组中为2的幂的元素数[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


输出:
2
1

时间复杂度: O(max(Q,N))