Count integers from a given range with no odd divisors | C ++ program for the above approach ; Function to count integers in the range 1 to N having no odd divisors ; Traverse the array ; Stores the nearest power of two less than arr [ i ] ; Stores the count of integers with no odd divisor for each query ; Iterate until powerOfTwo is less then or equal to arr [ i ] ; Print the answer for the current element ; Driver Code ; Given array ; Size of the array
#include <bits/stdc++.h> NEW_LINE 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 ; }