Sunday, October 12, 2025
HomeData Modelling & AICount of divisors having more set bits than quotient on dividing N

Count of divisors having more set bits than quotient on dividing N

Given a positive integer N. The task is to find the number of divisors (say d) which gives a quotient (say q) on an integer division (i.e q = N/d) such that the quotient has bits or equal set bits than the divisor. In other words, find the number of the possible values of ā€˜d’ which will produce ā€˜q’ on an integer dividing ā€˜n’ (q = N/d) such that ā€˜q’ has fewer or equal set bits (at least 1) than ā€˜d’.

Examples :Ā 

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 has 0 set bit so there is no point to go above n.Ā 
Also, for n/2 < d <= n, it will return q = 1, which has 1 set bit which will always be less than or equal to d. And, the minimum possible value of d is 1. So, the possible value of d is from 1 to n.Ā 
Now, observe by 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 sequences, one for increasing dĀ 
and the other for decreasing q. Now, observe, initially, the number of set bits of d is greater than q, but after a point, the number of set bits of d is less than or equal to q.Ā 
So, our problem is reduced to finding that point i.e. ā€˜x’ for the value of d, from 1 <= d <= n, such that q has less than or equal set bits to d.Ā 
So, the count of possible d such that q has less than or equal set bits of d will be equal to n – x + 1.

Below is the implementation of this approach:Ā 

C++




// C++ Program to find number of Divisors
// which on integer division produce quotient
// having less set bit than divisor
#include <bits/stdc++.h>
using namespace std;
Ā 
// Return the count of set bit.
int bit(int x)
{
Ā Ā Ā Ā int ans = 0;
Ā 
Ā Ā Ā Ā while (x) {
Ā Ā Ā Ā Ā Ā Ā Ā x /= 2;
Ā Ā Ā Ā Ā Ā Ā Ā ans++;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā return ans;
}
Ā 
// check if q and d have same number of set bit.
bool check(int d, int x)
{
Ā Ā Ā Ā if (bit(x / d) <= bit(d))
Ā Ā Ā Ā Ā Ā Ā Ā return true;
Ā 
Ā Ā Ā Ā return false;
}
Ā 
// Binary Search to find the point at which
// number of set in q is less than or equal to d.
int bs(int n)
{
Ā Ā Ā Ā int l = 1, r = sqrt(n);
Ā 
Ā Ā Ā Ā // while left index is less than right index
Ā Ā Ā Ā while (l < r) {
Ā Ā Ā Ā Ā Ā Ā Ā // finding the middle.
Ā Ā Ā Ā Ā Ā Ā Ā int m = (l + r) / 2;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // check if q and d have same number of
Ā Ā Ā Ā Ā Ā Ā // set it or not.
Ā Ā Ā Ā Ā Ā Ā Ā if (check(m, n))
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā r = m;
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā l = m + 1;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā if (!check(l, n))
Ā Ā Ā Ā Ā Ā Ā Ā return l + 1;
Ā 
Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā return l;
}
Ā 
int countDivisor(int n)
{
Ā Ā Ā Ā return n - bs(n) + 1;
}
// Driven Program
int main()
{
Ā Ā Ā Ā int n = 5;
Ā Ā Ā Ā cout << countDivisor(n) << endl;
Ā 
Ā Ā Ā Ā return 0;
}


Java




// Java Program to find number
// of Divisors which on integer
// division produce quotient
// having less set bit than divisor
import java .io.*;
Ā 
class GFG
{
Ā 
// Return the count of set bit.
static int bit(int x)
{
Ā Ā Ā Ā int ans = 0;
Ā Ā Ā Ā while (x > 0)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā x /= 2;
Ā Ā Ā Ā Ā Ā Ā Ā ans++;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā return ans;
}
Ā 
// check if q and d have
// same number of set bit.
static boolean check(int d, int x)
{
Ā Ā Ā Ā if (bit(x / d) <= bit(d))
Ā Ā Ā Ā Ā Ā Ā Ā return true;
Ā 
Ā Ā Ā Ā return false;
}
Ā 
// Binary Search to find
// the point at which
// number of set in q is
// less than or equal to d.
static int bs(int n)
{
Ā Ā Ā Ā int l = 1, r = (int)Math.sqrt(n);
Ā 
Ā Ā Ā Ā // while left index is
Ā Ā Ā Ā // less than right index
Ā Ā Ā Ā while (l < r)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // finding the middle.
Ā Ā Ā Ā Ā Ā Ā Ā int m = (l + r) / 2;
Ā 
Ā Ā Ā Ā // check if q and d have
Ā Ā Ā Ā // same number of
Ā Ā Ā Ā // set it or not.
Ā Ā Ā Ā Ā Ā Ā Ā if (check(m, n))
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā r = m;
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā l = m + 1;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā if (!check(l, n))
Ā Ā Ā Ā Ā Ā Ā Ā return l + 1;
Ā 
Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā return l;
}
Ā 
static int countDivisor(int n)
{
Ā Ā Ā Ā return n - bs(n) + 1;
}
Ā 
Ā Ā Ā Ā // Driver Code
Ā Ā Ā Ā static public void main (String[] args)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā int n = 5;
Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(countDivisor(n));
Ā Ā Ā Ā }
}
Ā 
// This code is contributed by anuj_67.


Python3




# Python3 Program to find number
# of Divisors which on integer
# division produce quotient
# having less set bit than divisor
import math
# Return the count of set bit.
def bit(x) :
Ā Ā Ā Ā ans = 0
Ā Ā Ā Ā while (x) :
Ā Ā Ā Ā Ā Ā Ā Ā x /= 2
Ā Ā Ā Ā Ā Ā Ā Ā ans = ans + 1
Ā 
Ā Ā Ā Ā return ans
Ā 
# check if q and d have
# same number of set bit.
def check(d, x) :
Ā Ā Ā Ā if (bit(x /d) <= bit(d)) :
Ā Ā Ā Ā Ā Ā Ā Ā return True
Ā 
Ā Ā Ā Ā return False;
Ā Ā Ā Ā Ā 
# Binary Search to find
# the point at which
# number of set in q is
# less than or equal to d.
def bs(n) :
Ā Ā Ā Ā l = 1
Ā Ā Ā Ā r = int(math.sqrt(n))
Ā 
Ā Ā Ā Ā # while left index is less
Ā Ā Ā Ā # than right index
Ā Ā Ā Ā while (l < r) :Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # finding the middle.
Ā Ā Ā Ā Ā Ā Ā Ā m = int((l + r) / 2)
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # check if q and d have
Ā Ā Ā Ā Ā Ā Ā Ā # same number of
Ā Ā Ā Ā Ā Ā Ā Ā # set it or not.
Ā Ā Ā Ā Ā Ā Ā Ā if (check(m, n)) :
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā r = m
Ā Ā Ā Ā Ā Ā Ā Ā else :
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā l = m + 1
Ā 
Ā Ā Ā Ā if (check(l, n) == False) :
Ā Ā Ā Ā Ā Ā Ā Ā return math.floor(l + 1)
Ā Ā Ā Ā else :
Ā Ā Ā Ā Ā Ā Ā Ā return math.floor(l)
Ā 
def countDivisor(n) :
Ā Ā Ā Ā return n - bs(n) + 1
Ā 
# Driver Code
n = 5
print (countDivisor(n))
Ā 
# This code is contributed by
# Manish Shaw (manishshaw1)


C#




// C# Program to find number of Divisors
// which on integer division produce quotient
// having less set bit than divisor
using System;
class GFG {
Ā 
// Return the count of set bit.
static int bit(int x)
{
Ā Ā Ā Ā int ans = 0;
Ā Ā Ā Ā while (x > 0) {
Ā Ā Ā Ā Ā Ā Ā Ā x /= 2;
Ā Ā Ā Ā Ā Ā Ā Ā ans++;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā return ans;
}
Ā 
// check if q and d have
// same number of set bit.
static bool check(int d, int x)
{
Ā Ā Ā Ā if (bit(x / d) <= bit(d))
Ā Ā Ā Ā Ā Ā Ā Ā return true;
Ā 
Ā Ā Ā Ā return false;
}
Ā 
// Binary Search to find
// the point at which
// number of set in q is
// less than or equal to d.
static int bs(int n)
{
Ā Ā Ā Ā int l = 1, r = (int)Math.Sqrt(n);
Ā 
Ā Ā Ā Ā // while left index is
Ā Ā Ā Ā // less than right index
Ā Ā Ā Ā while (l < r) {
Ā Ā Ā Ā Ā Ā Ā Ā // finding the middle.
Ā Ā Ā Ā Ā Ā Ā Ā int m = (l + r) / 2;
Ā 
Ā Ā Ā Ā // check if q and d have
Ā Ā Ā Ā // same number of
Ā Ā Ā Ā // set it or not.
Ā Ā Ā Ā Ā Ā Ā Ā if (check(m, n))
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā r = m;
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā l = m + 1;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā if (!check(l, n))
Ā Ā Ā Ā Ā Ā Ā Ā return l + 1;
Ā 
Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā return l;
}
Ā 
static int countDivisor(int n)
{
Ā Ā Ā Ā return n - bs(n) + 1;
}
Ā 
Ā Ā Ā Ā // Driver Code
Ā Ā Ā Ā static public void Main ()
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā int n = 5;
Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(countDivisor(n));
Ā Ā Ā Ā }
}
Ā 
// This code is contributed by anuj_67.


PHP




<?php
// PHP Program to find number of Divisors
// which on integer division produce quotient
// having less set bit than divisor
Ā 
// Return the count of set bit.
function bit( $x)
{
Ā Ā Ā Ā $ans = 0;
Ā 
Ā Ā Ā Ā while ($x)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā $x /= 2;
Ā Ā Ā Ā Ā Ā Ā Ā $ans++;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā return $ans;
}
Ā 
// check if q and d have
// same number of set bit.
function check( $d, $x)
{
Ā Ā Ā Ā if (bit($x /$d) <= bit($d))
Ā Ā Ā Ā Ā Ā Ā Ā return true;
Ā 
Ā Ā Ā Ā return false;
}
Ā 
// Binary Search to find
// the point at which
// number of set in q is
// less than or equal to d.
function bs(int $n)
{
Ā Ā Ā Ā $l = 1;
Ā Ā Ā Ā $r = sqrt($n);
Ā 
Ā Ā Ā Ā // while left index is less
Ā Ā Ā Ā // than right index
Ā Ā Ā Ā while ($l < $r)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // finding the middle.
Ā Ā Ā Ā Ā Ā Ā Ā $m = ($l + $r) / 2;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // check if q and d have
Ā Ā Ā Ā Ā Ā Ā Ā // same number of
Ā Ā Ā Ā Ā Ā Ā Ā // set it or not.
Ā Ā Ā Ā Ā Ā Ā Ā if (check($m, $n))
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $r = $m;
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $l = $m + 1;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā if (!check($l, $n))
Ā Ā Ā Ā Ā Ā Ā Ā return floor($l + 1);
Ā 
Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā return floor($l);
}
Ā 
function countDivisor( $n)
{
Ā Ā Ā Ā return $n - bs($n) + 1;
}
Ā 
Ā Ā Ā Ā // Driver Code
Ā Ā Ā Ā $n = 5;
Ā Ā Ā Ā echo countDivisor($n) ;
Ā 
// This code is contributed by anuj_67.
?>


Javascript




<script>
Ā Ā Ā Ā // Javascript Program to find number of Divisors
Ā Ā Ā Ā // which on integer division produce quotient
Ā Ā Ā Ā // having less set bit than divisor
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā // Return the count of set bit.
Ā Ā Ā Ā function bit(x)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā let ans = 0;
Ā Ā Ā Ā Ā Ā Ā Ā while (x > 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā x = parseInt(x / 2, 10);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans++;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā return ans;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // check if q and d have
Ā Ā Ā Ā // same number of set bit.
Ā Ā Ā Ā function check(d, x)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā if (bit(parseInt(x / d, 10)) <= bit(d))
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return true;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā return false;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Binary Search to find
Ā Ā Ā Ā // the point at which
Ā Ā Ā Ā // number of set in q is
Ā Ā Ā Ā // less than or equal to d.
Ā Ā Ā Ā function bs(n)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā let l = 1, r = Math.sqrt(n);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // while left index is
Ā Ā Ā Ā Ā Ā Ā Ā // less than right index
Ā Ā Ā Ā Ā Ā Ā Ā while (l < r) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // finding the middle.
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let m = parseInt((l + r) / 2, 10);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // check if q and d have
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // same number of
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // set it or not.
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (check(m, n))
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā r = m;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā l = m + 1;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if (!check(l, n))
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return l + 1;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return l;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā function countDivisor(n)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā return n - bs(n) + 1;
Ā Ā Ā Ā }
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā let n = 5;
Ā Ā Ā Ā document.write(countDivisor(n));
Ā 
</script>


Output:Ā 

4

Ā 

Time Complexity: O(logN)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32353 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6721 POSTS0 COMMENTS
Nicole Veronica
11885 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11943 POSTS0 COMMENTS
Shaida Kate Naidoo
6841 POSTS0 COMMENTS
Ted Musemwa
7105 POSTS0 COMMENTS
Thapelo Manthata
6797 POSTS0 COMMENTS
Umr Jansen
6798 POSTS0 COMMENTS