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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:16:06+00:00 2026-05-19T23:16:06+00:00

I am having a general problem finding a good algorithm for generating each possible

  • 0

I am having a general problem finding a good algorithm for generating each possible assignment for some integers in different arrays.

Lets say I have n arrays and m numbers (I can have more arrays than numbers, more numbers than arrays or as much arrays as numbers).

As an example I have the numbers 1,2,3 and three arrays:

{ }, { }, { }

Now I would like to find each of these solutions:

{1,2,3}, { }, { }
{ }, {1,2,3}, { }
{ }, { }, {1,2,3}
{1,2}, {3}, { }
{1,2}, { }, {3}
{ }, {1,2}, {3}
{1}, {2,3}, { }
{1}, { }, {2,3}
{ }, {1}, {2,3}
{1}, {2}, {3}

So basically I would like to find each possible combination to assign the numbers to the different arrays with keeping the order. So as in the example the 1 always needs to come before the others and so on…

I want to write an algorithm in C++/Qt to find all these valid combinations.

Does anybody have an approach for me on how to handle this problem? How would I generate these permutations?

ADDITIONS

Unfortunately I didn’t manage to change the great examples you gave for the problem I have now, since the numbers that I want to arrange in the arrays are stored in an array (or for me a QVector)

Can anybody help me change the algorithm so that it gives me each possible valid combination of the numbers in the QVector to the QVector< QVector > so that I can do further computations on each one?

QVector<int> line; // contains the numbers: like {7,3,6,2,1}
QVector< QVector<int> > buckets; // empty buckets for the numbers { {}, {}, {} }

QList< QVector< QVector<int> > > result; // List of all possible results

Would be really great if anyone could provide me with a simple implementation that works or tips on how to get it… I just couldn’t change the code that was already provided so that it works…

  • 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-19T23:16:07+00:00Added an answer on May 19, 2026 at 11:16 pm

    The following code is written in C#.

    class LineList<T> : List<T[][]> 
    {
        public override string ToString()
        {
            var sb = new StringBuilder();
            sb.Append(Count).AppendLine(" lines:");
            foreach (var line in this)
            {
                if (line.Length > 0)
                {
                    foreach (var bucket in line)
                    {
                        sb.Append('{');
                        foreach (var item in bucket)
                        {
                            sb.Append(item).Append(',');
                        }
                        if (bucket.Length > 0)
                        {
                            sb.Length -= 1;
                        }
                        sb.Append("}, ");
                    }
                    sb.Length -= 2;
                }
                sb.AppendLine();
            }
            return sb.ToString();
        }
    }
    
    class Permutor<T>
    {
        public T[] Items { get; private set; }
        public bool ReuseBuckets { get; set; }
        private T[] emptyBucket_;
        private Dictionary<int, Dictionary<int, T[]>> buckets_;   // for memoization when ReuseBuckets=true
        public Permutor(T[] items)
        {
            ReuseBuckets = true;  // faster and uses less memory
            Items = items;
            emptyBucket_ = new T[0];
            buckets_ = new Dictionary<int, Dictionary<int, T[]>>();
        }
        private T[] GetBucket(int index, int size)
        {
            if (size == 0)
            {
                return emptyBucket_;
            }
            Dictionary<int, T[]> forIndex;
            if (!buckets_.TryGetValue(index, out forIndex))
            {
                forIndex = new Dictionary<int, T[]>();
                buckets_[index] = forIndex;
            }
            T[] bucket;
            if (!forIndex.TryGetValue(size, out bucket))
            {
                bucket = new T[size];
                Array.Copy(Items, index, bucket, 0, size);
                forIndex[size] = bucket;
            }
            return bucket;
        }
        public LineList<T> GetLines(int bucketsPerLine)
        {
            var lines = new LineList<T>();
            if (bucketsPerLine > 0)
            {
                AddLines(lines, bucketsPerLine, 0);
            }
            return lines;
        }
        private void AddLines(LineList<T> lines, int bucketAllotment, int taken)
        {
            var start = bucketAllotment == 1 ? Items.Length - taken : 0;
            var stop = Items.Length - taken;
            for (int nItemsInNextBucket = start; nItemsInNextBucket <= stop; nItemsInNextBucket++)
            {
                T[] nextBucket;
                if (ReuseBuckets)
                {
                    nextBucket = GetBucket(taken, nItemsInNextBucket);
                }
                else
                {
                    nextBucket = new T[nItemsInNextBucket];
                    Array.Copy(Items, taken, nextBucket, 0, nItemsInNextBucket);
                }
                if (bucketAllotment > 1)
                {
                    var subLines = new LineList<T>();
                    AddLines(subLines, bucketAllotment - 1, taken + nItemsInNextBucket);
                    foreach (var subLine in subLines)
                    {
                        var line = new T[bucketAllotment][];
                        line[0] = nextBucket;
                        subLine.CopyTo(line, 1);
                        lines.Add(line);
                    }
                }
                else
                {
                    var line = new T[1][];
                    line[0] = nextBucket;
                    lines.Add(line);
                }
            }
        }
    
    }
    

    These calls…

    var permutor = new Permutor<int>(new[] { 1, 2, 3 });
    for (int bucketsPerLine = 0; bucketsPerLine <= 4; bucketsPerLine++)
    {
        Console.WriteLine(permutor.GetLines(bucketsPerLine));
    }
    

    generate this output…

    0 lines:
    
    1 lines:
    {1,2,3}
    
    4 lines:
    {}, {1,2,3}
    {1}, {2,3}
    {1,2}, {3}
    {1,2,3}, {}
    
    10 lines:
    {}, {}, {1,2,3}
    {}, {1}, {2,3}
    {}, {1,2}, {3}
    {}, {1,2,3}, {}
    {1}, {}, {2,3}
    {1}, {2}, {3}
    {1}, {2,3}, {}
    {1,2}, {}, {3}
    {1,2}, {3}, {}
    {1,2,3}, {}, {}
    
    20 lines:
    {}, {}, {}, {1,2,3}
    {}, {}, {1}, {2,3}
    {}, {}, {1,2}, {3}
    {}, {}, {1,2,3}, {}
    {}, {1}, {}, {2,3}
    {}, {1}, {2}, {3}
    {}, {1}, {2,3}, {}
    {}, {1,2}, {}, {3}
    {}, {1,2}, {3}, {}
    {}, {1,2,3}, {}, {}
    {1}, {}, {}, {2,3}
    {1}, {}, {2}, {3}
    {1}, {}, {2,3}, {}
    {1}, {2}, {}, {3}
    {1}, {2}, {3}, {}
    {1}, {2,3}, {}, {}
    {1,2}, {}, {}, {3}
    {1,2}, {}, {3}, {}
    {1,2}, {3}, {}, {}
    {1,2,3}, {}, {}, {}
    

    The approximate size of the solution (bucketsPerLine * NumberOfLines) dominates the execution time. For these tests, N is the length of the input array and the bucketsPerLine is set to N as well.

    N=10, solutionSize=923780, elapsedSec=0.4, solutionSize/elapsedMS=2286
    N=11, solutionSize=3879876, elapsedSec=2.1, solutionSize/elapsedMS=1835
    N=12, solutionSize=16224936, elapsedSec=10.0, solutionSize/elapsedMS=1627
    N=13, solutionSize=67603900, elapsedSec=47.9, solutionSize/elapsedMS=1411
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In general, what kinds of design decisions help an application scale well? (Note: Having
Having a problem getting a TreeView control to display node images. The code below
I am new to Blackberry development.My application contain five list items having strings each
Having considerable trouble with some pointer arithmatic. I think I get the concepts (pointer
I am having a problem in ScrollView. When i run my application on X10i
I seem to be having a problem with JTextPane. I have extended JTextPane to
I'm having a problem using Git. Bear in mind I'm very new to Git.
Having been a PHP developer on LAMP servers for quite a while, is there
Having a heckuva time with this one, though I feel I'm missing something obvious.
Having worked with Classic ASP for about 2 years now by creating a few

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.