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

📅  最后修改于: 2021-05-14 02:54:47             🧑  作者: Mango

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

例子:

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

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

观察:

因此,该问题简化为找到整数的计数范围为1N,使得这些数字是二的幂,其可以在对数(N)的时间复杂度通过反复检查i的两个这样的即2的幂来进行≤ N ,每一步都在增加。

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

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

下面是上述方法的实现:

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)