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

  • Home
  • SEARCH
  • 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 6940119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:42:43+00:00 2026-05-27T12:42:43+00:00

Question same as in the title. I’ve done two approaches. One is straightforward. Generate

  • 0

Question same as in the title.
I’ve done two approaches. One is straightforward.
Generate all bitmasks from

2^{n-1}

to

2^n

And for every bitmask check if there is same amount 1’s and 0’s, if yes, work on it.
And that’s the problem, because i have to work on those bitmasks not only count them.

I came with second approach which runs on O(2^{n/2}) time, but seems like it’s not generating all bitmasks and i don’t know why.

Second approach is like that :
generate all bitmasks from 0 to 2^{n/2} and to have valid bitmask( call it B ) i have to do something like this : B#~B

where ~ is negative.

So for example i have n=6, so i’m going to generate bitmasks with length of 3.

For example i have B=101, so ~B will be 010
and final bitmask would be 101010, as we see, we have same amount of 1’s and 0’s.

Is this method good or am i implementing something bad ? Maybe some another interesting approach exist?
Thanks

Chris

  • 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-05-27T12:42:44+00:00Added an answer on May 27, 2026 at 12:42 pm

    Try a recursive approach:

    void printMasks(int n0, int n1, int mask) {
        if (!n0 && !n1) {
            cerr << mask << endl;
            return;
        }
        mask <<= 1;
        if (n0) {
            printMasks(n0-1, n1, mask);
        }
        if (n1) {
            printMasks(n0, n1-1, mask | 1);
        }
    }
    

    Call printMasks passing it the desired number of 0’s and 1’s. For example, if you need 3 ones and 3 zeros, call it like this:

    printMasks(3, 3, 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question is same as the title. I have not done Test driven development
First of all there is a question with the same title here on SO
There is a question here in stackoverflow with the same title but that is
I know that question with same title has been asked almost 6 month ago.
I have question with this same title here but now as I'll present in
My question is the same as the title. I just want to know if
EDIT : Changed title from inheritance to composition. Left body of question unchanged. I'm
Again same question title but requirement is somewhat different. We have implemented push notification
So I asked a question with the same title earlier and got an answer
Same question as title. Not a real problem I'm having or anything, I was

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.