Count integers from a given range with no odd divisors
Last Updated :
02 Aug, 2022
Given an array arr[] consisting of N integers, the task is to count the number of integers in the range [1, arr[i]] that does not contain any odd divisors.
Examples:
Input: arr[] = {15, 16, 20, 35}
Output: 3 4 4 5
Explanation:
Numbers with no odd divisors from 1 to arr[0] ( = 15), are 2, 4, 8. Therefore, the count is 3.
Similarly, for arr[1] (= 16), arr[2] (= 20), arr[3] (= 35) the count is 4, 4 and 5 respectively.
Input: arr[] = {24}
Output: 4
Naive Approach: The simplest approach is to traverse the array and for each array element arr[i], check if a number has any odd divisors in the range [1, arr[i]] or not. If it doesn’t have any odd divisors, increment the count. Otherwise, proceed to the next integer. Finally, print the count obtained for each array element arr[i].
Time Complexity: O(N*max(arr[i]))
Auxiliary Space: O(1)
Efficient Approach: The optimal idea is based on the following observation:
Observation:
Any number can be represented in the form of p1x1 * p2x2 *- – -*pNxN, where pi is a prime number and xi is its power. If the number doesn’t have any odd divisor, then it should not contain any odd prime factor.
Therefore, the problem reduces to finding the count of integers in the range 1 to N such that those numbers are a power of two, which can be done in log(N) time complexity by repeatedly checking for power of two such that 2i ? N and increasing count in every step.
Follow the steps below to solve the problem:
- Traverse the array arr[] and perform the following operations:
- Initialize two variables, say powerOfTwo, to check the maximum power of 2 which is less than arr[i] and another variable, say count, to store the count of integers in the range [1, arr[i]] having no odd divisors.
- Find for each array element, the value of powerOfTwo and count.
- Print them as the answer for each array element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void oddDivisors( int arr[], int N)
{
for ( int i = 0; i < N; i++) {
int powerOfTwo = 2;
int count = 0;
while (powerOfTwo <= arr[i]) {
count++;
powerOfTwo = 2 * powerOfTwo;
}
cout << count << " " ;
}
return ;
}
int main()
{
int arr[] = { 15, 16, 20, 35 };
int N = sizeof (arr) / sizeof (arr[0]);
oddDivisors(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void oddDivisors( int arr[], int N)
{
for ( int i = 0 ; i < N; i++)
{
int powerOfTwo = 2 ;
int count = 0 ;
while (powerOfTwo <= arr[i])
{
count++;
powerOfTwo = 2 * powerOfTwo;
}
System.out.print(count + " " );
}
return ;
}
public static void main(String args[])
{
int arr[] = { 15 , 16 , 20 , 35 };
int N = arr.length;
oddDivisors(arr, N);
}
}
|
Python3
def oddDivisors(arr, N) :
for i in range (N) :
powerOfTwo = 2 ;
count = 0 ;
while (powerOfTwo < = arr[i]) :
count + = 1 ;
powerOfTwo = 2 * powerOfTwo;
print (count, end = " " );
if __name__ = = "__main__" :
arr = [ 15 , 16 , 20 , 35 ];
N = len (arr);
oddDivisors(arr, N);
|
C#
using System;
class GFG
{
static void oddDivisors( int [] arr, int N)
{
for ( int i = 0; i < N; i++)
{
int powerOfTwo = 2;
int count = 0;
while (powerOfTwo <= arr[i])
{
count++;
powerOfTwo = 2 * powerOfTwo;
}
Console.Write(count + " " );
}
return ;
}
public static void Main(String[] args)
{
int [] arr = { 15, 16, 20, 35 };
int N = arr.Length;
oddDivisors(arr, N);
}
}
|
Javascript
<script>
function oddDivisors(arr , N) {
for (i = 0; i < N; i++) {
var powerOfTwo = 2;
var count = 0;
while (powerOfTwo <= arr[i]) {
count++;
powerOfTwo = 2 * powerOfTwo;
}
document.write(count + " " );
}
return ;
}
var arr = [ 15, 16, 20, 35 ];
var N = arr.length;
oddDivisors(arr, N);
</script>
|
Time Complexity: O(N * log(max(arr[i]))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...