Given a positive integer N. The task is to find the number of divisor (say d) which give quotient (say q) on integer division (i.e q = N/d) such that quotient have less or equal set bit than divisor. In other words, find number of possible value of ‘d’ which will produce ‘q’ on integer dividing ‘n’ (q = N/d) such that ‘q’ have less or equal set bits (atleast 1) than ‘d’.
Input : N = 5 Output : 4 for d = 1 (set bit = 1), q = 5/1 = 5 (set bit = 2), count = 0. for d = 2 (set bit = 1), q = 5/2 = 2 (set bit = 1), count = 1. for d = 3 (set bit = 2), q = 5/3 = 1 (set bit = 1), count = 2. for d = 4 (set bit = 1), q = 5/4 = 1 (set bit = 1), count = 3. for d = 5 (set bit = 2), q = 5/5 = 1 (set bit = 1), count = 4. Input : N = 3 Output : 2
Observe, for all d > n, q have 0 set bit so there is no point to go above n.
Also, for n/2 < d <= n, it will return q = 1, which have 1 set bit which will always be less
than or equal to d. And, minimum possible value of d is 1. So, possible value of d is from 1 to n.
Now, observe on dividing n by d, where 1 <= d <= n, it will give q in sorted order (decreasing).
So, with increasing d we will get decreasing q. So, we get the two sorted sequence, one for increasing d
and other for decreasing q. Now, observe initially the number of set bit of d is greater than q but after
a point number of set bit of d is less than or equal to q.
So, our problem reduce to find that point i.e. ‘x’ for the value of d, from 1 <= d <= n, such that q have less than or equal set bit to d.
So, count of possible d such that q have less than or equal set bit to d will be equal to n – x + 1.
Below is the implementation of this approach :
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Quotient and remainder dividing by 2^k (a power of 2)
- Check if count of even divisors of N is equal to count of odd divisors
- Check if bits of a number has count of consecutive set bits in increasing order
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
- Count Divisors of n in O(n^1/3)
- Check if count of divisors is even or odd
- Count Divisors of Factorial
- Count of the non-prime divisors of a given number
- Count all perfect divisors of a number
- C Program to Check if count of divisors is even or odd
- Count total divisors of A or B in a given range
- Count divisors of array multiplication
- Count of Fibonacci divisors of a given number
- Count of numbers below N whose sum of prime divisors is K
- Count divisors of n that have at-least one digit common with n
- Count number of digits after decimal on dividing a number
- Count all pairs of divisors of a number N whose sum is coprime with N
- Java Program to Check if count of divisors is even or odd
- Count of square free divisors of a given number
- Maximum count of common divisors of A and B such that all are co-primes to one another
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.