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 7942247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:54:48+00:00 2026-06-03T23:54:48+00:00

Here is the excise: You start with an empty room and a group of

  • 0

Here is the excise:

You start with an empty room and a group of n people waiting outside. At each step, you may either admit one person into the room, or let one out. Can you arrange a sequence of 2n steps, so that every possible combination of people is achieved exactly once?


My solution is:

I can have a bit array which has n elements. Each element’s status stands for whether this person is in the room or not. So totally we will have 2n different combinations of people in the room.

The algorithm can be a standard backtrack to list out all the combinations.


I am just wondering whether my thought is too naive or simple?

Any trap in this excise?


Edit:

For people who are interested in the implementation of gray code, please see

http://yagni.com/graycode/

  • 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-03T23:54:50+00:00Added an answer on June 3, 2026 at 11:54 pm

    The algorithm can be a standard backtrack to list out all the combinations.

    Your solution “works”, but if implemented naively, it will requires a lot more than 2n steps.

    See the following sentence in the problem statement:

    At each step, you may either admit one person into the room, or let one out.

    In your solution, when you list all bit-vectors, you’ll probably have 0111 followed by 1000 which means that three people will have to leave the room, and one person will have to enter.

    That requires 4 steps, and not one, thus you’ll get a lot more than 2n steps to run through all combinations.

    However, you can arrange the bit-vectors you describe, in a way such that only one bit differs between two consecutive vectors. This is what is refered to as Gray code.

    Here’s an example from the Wikipedia article:

    Dec  Gray   Binary
     0   000    000
     1   001    001
     2   011    010
     3   010    011
     4   110    100
     5   111    101
     6   101    110
     7   100    111
    

    Notice how

    1. all bit-vectors are covered, and
    2. for each consecutive vector, only one bit changes.

    This means that you’ll be able to iterate through all combinations in precisely 2n steps.

    How to implement such sequence generator is also explained in the Wikipedia page, but if it’s an exercise, I suggest you make an attempt yourself before peeking 😉

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

Sidebar

Related Questions

Here is my question: #data 1: lab1 <- 1:10 group <- rep(1:3, each =
Here's the scenario: I'm an SSIS virgin, but I found an excuse to start
Here is an excise Suppose we are given the minimum spanning tree T of
Here is an excise: Consider the problem of finding a minimum weight connected subset
Here is an excise: In certain graph problems, vertices have can have weights instead
Here is a excise from The book Algorithm Design Manual . In the bin-packing
Here's one I have always wondered about... Please excuse my naivety, but - How
Here is an excise for graph. Given an undirected graph G with n vertices
Here's what I have so far: var bestReason = from p in successfulReasons group
I'm not sure how many people who use Vista Style Builder are on 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.