Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8505521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:20:41+00:00 2026-06-11T02:20:41+00:00

I found this question here , but this is not the answer i am

  • 0

I found this question here, but this is not the answer i am looking for. Hence, posting again.

A function of the form:

F( N ) = rank

means that Given a number N in decimal representation, its rank is given as:

Starting from 0 to N, how many numbers are there with same number of set bits in its
binary representation.

I will go through an example to make it more clear.

N = 6 ( 0110 )
Its rank is 3.
1. 3 ( 0011 )
2. 5 ( 0101 )
3. 6 ( 0110 )

Now, given a number, find its rank.

The obvious approach is to start from 0 and check for number of set bits for each number till N-1.

The question is:

Is there any logN solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:20:42+00:00Added an answer on June 11, 2026 at 2:20 am

    Yes, there is a log n solution.

    Let n have k bits set, the most significant bit being in position p (starting to count positions from 0, so 2^p <= n < 2^(p+1)). Then there are pCk (binomial coefficient, also choose(p,k)) ways to place k bits in the positions 0, ..., p-1, all these give numbers with exactly k set bits that are smaller than n. If k == 1, that’s it, otherwise there remain the numbers with k set bits and the p-th bit set that are smaller than n to consider. Those can be counted by determining the rank of n - 2^p.

    Code (not optimal, does some unnecessary recomputation, and doesn’t all that it could do to avoid overflow):

    unsigned long long binom(unsigned n, unsigned k) {
        if (k == 0 || n == k) return 1;
        if (n < k) return 0;
        if (k > (n >> 1)) k = n-k;
        unsigned long long res = n, j;
        // We're not doing all we can to avoid overflow, as this is a proof of concept,
        // not production code.
        for(j = 2; j <= k; ++j) {
            res *= (n+1-j);
            res /= j;
        }
        return res;
    }
    
    unsigned popcount(unsigned long long n) {
        unsigned k = 0;
        while(n) {
            ++k;
            n &= (n-1);
        }
        return k;
    }
    
    unsigned long long rank(unsigned long long n) {
        if (n == 0) return 1;
        unsigned p = 0, k = popcount(n);
        unsigned long long mask = 1,h = n >> 1;
        while(h > 0) {
            ++p;
            h >>= 1;
        }
        mask <<= p;
        unsigned long long r = binom(p,k);
        r += rank(n-mask);
        return r;
    }
    

    Tested in a loop for 0 <= n < 10^8 to check for mistakes without any mismatch found.

    Check output here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this similar question here , but this is really old. Was it
I can't resolve this issue, I found a similar question here but: setting the
Searching here I found that this question was already asked , but I think
I found this question here: OLEDB v/s ODBC Which gave me more information, but
I've searched around for answers to this question, but not found anything quite on
Aloha, I found an answer to my question here . Basically i'm looking to
I found this question , which has an answer, but facebook changed the token
I've found some questions here about this topic, but none of the solutions solve
I found a few questions similar to this one here on SO, but none
I haven't found this question, feel free to close if it's already up here.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.