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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:39:29+00:00 2026-05-23T14:39:29+00:00

I would like to write a function that generate an array of tuples containing

  • 0

I would like to write a function that generate an array of tuples containing all possible permutations of N balls in M boxes in C++.

The order (Edit : in the resulting list) is not important, just that the first must be (N,0,…,0) and the last (0,0,…,N).

I didn’t find such an implementation on the net in C++, only permutations of chars or calculations of the number of permutations…

Any ideas ?

  • 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-23T14:39:30+00:00Added an answer on May 23, 2026 at 2:39 pm

    There’s a neat trick to solving this. Imagine that we took the n balls and m − 1 boxes and put them in a row of length n + m − 1 (with the boxes mixed up among the balls). Then put each ball in the box to its right, and add an mth box at the right that gets any balls that are left over.

    row containing a box, two balls, a box, one ball, a box, one ball, and an imaginary box

    This yields an arangement of n balls in m boxes.

    row of boxes containing zero, two, one, and one ball respectively

    It is easy to see that there are the same number of arrangements of n balls in sequence with m − 1 boxes (the first picture) as there are arrangements of n balls in m boxes. (To go one way, put each ball in the box to its right; to go the other way, each box empties the balls into the positions to its left.) Each arrangement in the first picture is determined by the positions where we put the boxes. There are m − 1 boxes and n + m − 1 positions, so there are n + m − 1Cm − 1 ways to do that.

    So you just need an ordinary combinations algorithm (see this question) to generate the possible positions for the boxes, and then take the differences between successive positions (less 1) to count the number of balls in each box.

    In Python it would be very simple since there’s a combinations algorithm in the standard library:

    from itertools import combinations
    
    def balls_in_boxes(n, m):
        """Generate combinations of n balls in m boxes.
    
        >>> list(balls_in_boxes(4, 2))
        [(0, 4), (1, 3), (2, 2), (3, 1), (4, 0)]
        >>> list(balls_in_boxes(3, 3))
        [(0, 0, 3), (0, 1, 2), (0, 2, 1), (0, 3, 0), (1, 0, 2), (1, 1, 1), (1, 2, 0), (2, 0, 1), (2, 1, 0), (3, 0, 0)]
    
        """
        for c in combinations(range(n + m - 1), m - 1):
            yield tuple(b - a - 1 for a, b in zip((-1,) + c, c + (n + m - 1,)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to write a function that would have some optional code to be
In Python, I'd like to write a function that would pretty-print its results to
I would like to write a cross-platform function in C++ that contains system calls.
I would like to write a JavaScript function that validates a zip code, by
I would like to write a function which will read values from a text
In C++0x I would like to write a function like this: template <typename... Types>
I would like to write this as a user defined function: private double Score(Story
I would like to write a plug-in that will allow a custom written CRM
I'm looking to write a function that takes an array of pages/categories (from a
I'm trying to write a Powershell function that takes an array argument. I want

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.