📌  相关文章
📜  计算给定范围内没有奇数除数的整数

📅  最后修改于: 2021-09-05 11:59:24             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是计算范围[1, arr[i]]中不包含任何奇数除数的整数的数量。

例子:

朴素的方法:最简单的方法是遍历数组,对于每个数组元素arr[i] ,检查一个数字是否在[1, arr[i]]范围内有任何奇数除数。如果它没有任何奇数除数,则增加计数。否则,继续下一个整数。最后,打印为每个数组元素arr[i]获得的计数。
时间复杂度: O(N*max(arr[i]))
辅助空间: O(1)

高效的方法:最佳想法基于以下观察:

观察:

因此,问题简化为找到1N范围内的整数的数量,使得这些数字是 2 的幂,这可以通过重复检查 2 的幂使得2 i以 log(N) 时间复杂度完成N并在每一步增加计数。

请按照以下步骤解决问题:

  • 遍历数组arr[]并执行以下操作:
    • 初始化两个变量,比如powerOfTwo ,以检查小于arr[i]2的最大幂和另一个变量,比如count ,用于存储范围[1, arr[i]]中没有奇数除数的整数的计数.
    • 为每个数组元素查找powerOfTwocount的值。
    • 将它们打印为每个数组元素的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count integers in the
// range 1 to N having no odd divisors
void oddDivisors(int arr[], int N)
{
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Stores the nearest power
        // of two less than arr[i]
        int powerOfTwo = 2;
 
        // Stores the count of integers
        // with no odd divisor for each query
        int count = 0;
 
        // Iterate until powerOfTwo is
        // less then or equal to arr[i]
        while (powerOfTwo <= arr[i]) {
 
            count++;
            powerOfTwo = 2 * powerOfTwo;
        }
 
        // Print the answer for
        // the current element
        cout << count << " ";
    }
 
    return;
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 15, 16, 20, 35 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    oddDivisors(arr, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to count integers in the
// range 1 to N having no odd divisors
static void oddDivisors(int arr[], int N)
{
   
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
        // Stores the nearest power
        // of two less than arr[i]
        int powerOfTwo = 2;
 
        // Stores the count of integers
        // with no odd divisor for each query
        int count = 0;
 
        // Iterate until powerOfTwo is
        // less then or equal to arr[i]
        while (powerOfTwo <= arr[i])
        {
            count++;
            powerOfTwo = 2 * powerOfTwo;
        }
 
        // Print the answer for
        // the current element
        System.out.print(count + " ");
    }
    return;
}
 
// Driver Code
public static void main(String args[])
{
    // Given array
    int arr[] = { 15, 16, 20, 35 };
 
    // Size of the array
    int N = arr.length;
    oddDivisors(arr, N);
}
}
 
// This code is contributed by sanjoy_62.


Python3
# Python3 program for the above approach
 
# Function to count integers in the
# range 1 to N having no odd divisors
def oddDivisors(arr, N) :
 
    # Traverse the array
    for i in range(N) :
 
        # Stores the nearest power
        # of two less than arr[i]
        powerOfTwo = 2;
 
        # Stores the count of integers
        # with no odd divisor for each query
        count = 0;
 
        # Iterate until powerOfTwo is
        # less then or equal to arr[i]
        while (powerOfTwo <= arr[i]) :
 
            count += 1;
            powerOfTwo = 2 * powerOfTwo;
 
        # Print the answer for
        # the current element
        print(count, end = " ");
 
# Driver Code
if __name__ == "__main__" :
 
    # Given array
    arr = [ 15, 16, 20, 35 ];
 
    # Size of the array
    N = len(arr);
 
    oddDivisors(arr, N);
 
    # This code is contributed by AnkThon


C#
// C# program to implement
// the above approach
using System;
class GFG
{
 
// Function to count integers in the
// range 1 to N having no odd divisors
static void oddDivisors(int[] arr, int N)
{
   
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
        // Stores the nearest power
        // of two less than arr[i]
        int powerOfTwo = 2;
 
        // Stores the count of integers
        // with no odd divisor for each query
        int count = 0;
 
        // Iterate until powerOfTwo is
        // less then or equal to arr[i]
        while (powerOfTwo <= arr[i])
        {
            count++;
            powerOfTwo = 2 * powerOfTwo;
        }
 
        // Print the answer for
        // the current element
        Console.Write(count + " ");
    }
    return;
}
 
// Driver Code
public static void Main(String[] args)
{
   
    // Given array
    int[] arr = { 15, 16, 20, 35 };
 
    // Size of the array
    int N = arr.Length;
    oddDivisors(arr, N);
}
}
 
// This code is contributed by sanjoy_62.


Javascript


输出:
3 4 4 5

时间复杂度: O(N * log(max(arr[i]))
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live