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 :
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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)
- Count Divisors of Factorial
- Check if count of divisors is even or odd
- Count of integers up to N which are non divisors and non coprime with N
- C Program to Check if count of divisors is even or odd
- Count of numbers below N whose sum of prime divisors is K
- Count of the non-prime divisors of a given number
- Count divisors of array multiplication
- Count total divisors of A or B in a given range
- Count all perfect divisors of a number
- Count of Fibonacci divisors of a given number
- Count divisors of n that have at-least one digit common with n
- Count number of digits after decimal on dividing a number
- Java Program to Check if count of divisors is even or odd
- Maximum count of common divisors of A and B such that all are co-primes to one another
- Sum of product of all integers upto N with their count of divisors
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.