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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:24:46+00:00 2026-06-04T13:24:46+00:00

I am trying to write a C# function that, given an argument like new

  • 0

I am trying to write a C# function that, given an argument like new int[] { 2, 3, 2 } which specifies the upper bound + 1 for each element, would return the following (via IEnumberable<int[]>):

0 0 0
0 0 1
0 1 0
0 2 0
1 0 0
0 1 1
0 2 1
1 0 1
1 1 0
1 2 0
1 1 1
1 2 1

Note that the order is important: all the permutations with 0 non-zero elements, followed by all those with 1 non-zero elements, etc. Within one of those groups the order doesn’t matter.

I realize that these may not technically be permutations, but it’s the closest term that I know of. Also I realize that one way would be to return all the permutations in some order and then sort them according to a function that counts how many non-zero elements there are, but I am hoping for something more elegant and efficient.

  • 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-04T13:24:48+00:00Added an answer on June 4, 2026 at 1:24 pm

    I wanted an answer that doesn’t calculate everything first and then sort, while still only going through things the minimal amount of times. Here’s what I’ve got. Note that externally modifying the int[] could screw up the results (alternately, could return a new int[]).

    The first method tells the helper method how many 0’s it wants in the output. The helper then calculates the results, stopping if it can’t fill in enough 0’s or if it runs through all the data.

    static IEnumerable<int[]> Permutation(int[] bounds)
    {
      for(int num0s = bounds.Length; num0s >= 0; --num0s)
      {
        foreach(int[] ret in PermHelper(num0s, 0, bounds, new int[bounds.Length]))
          yield return ret;
      }
    }
    
    static IEnumerable<int[]> PermHelper(int num0s, int index, int[] bounds, int[] result)
    {
      //Last index.
      if(index == bounds.Length - 1)
      {
        if(num0s > 0)
        {
          result[index] = 0;
          yield return result;
        }
        else
        {
          for(int i = 1; i < bounds[index]; ++i)
          {
            result[index] = i;
            yield return result;
          }
        }
      }
      //Others.
      else
      {
        //still need more 0s.
        if(num0s > 0)
        {
          result[index] = 0;
          foreach(int[] perm in PermHelper(num0s - 1, index + 1, bounds, result))
            yield return perm;
        }
        //Make sure there are enough 0s left if this one isn't a 0.
        if(num0s < bounds.Length - index)
        {
          for(int i = 1; i < bounds[index]; ++i)
          {
            result[index] = i;
            foreach(int[] perm in PermHelper(num0s, index + 1, bounds, result))
              yield return perm;
          }
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a function that takes multiple arguments, which can come either
I'm trying to write a function that would use its first argument, then send
I'm trying to write a function that will delete every row in a given
I'm trying to write a function that does something like this: >>> Give-me the
I'm trying to write a function that write-protects every pte in a given vm_area_struct
I'm trying to write a function that, given two quadtrees representing images, will output
I'm trying to write a function that takes a 2D vector as an argument.
I'm trying to write a function that calculates the grand total price of each
I'm trying to write a function that when given 2 arguments, the 2 leftmost
I'm trying to write a function that does the following: Given a text file,

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.