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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:04:56+00:00 2026-06-07T03:04:56+00:00

I am looking for an algorithm to find all combinations of K values to

  • 0

I am looking for an algorithm to find all combinations of K values to n items.

Example:

K values are [R,B] & N is 2 so i get {RR, RB, BR, BB} 2*2 = 4 ways

K values are [R,B] & N is 3 so i get {RRR, RRB, RBB, RBR, BRR, BRB, BBR, BBB} 2*2*2 = 8 ways

I need to find out the generic algorithm to find all possible ways in which K items can be arranged in N slots. (repeat is allowed)

Another example would be:

K values are [R,G,B] & N is 5 so i need to find 3^5 = 81 combinations.

  • 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-07T03:04:57+00:00Added an answer on June 7, 2026 at 3:04 am

    This problem lends itself exceptionally well to a recursive solution.

    The solution in the general case is clearly formed by taking the solution for N - 1, and then prepending each of your set’s elements in turn to the results. In pseudocode:

    f(options, 0) = []
    f(options, n) = options foreach o => o ++ f(options, n-1)
    

    This could be implemented recursively in Java, but you’d run into stack overflow errors for moderately large values of n; and I also suspect the JIT compiler is less effective at optimising recursive algorithms so performance would suffer.

    However, recursive algorithms can always be converted into a loop equivalent. In this case it might look something like:

    List<String> results = new ArrayList<String>();
    results.add(""); // Seed it for the base case n=0
    for (int i = 0; i < n; i ++) {
        List<String> previousResults = results;
        results = new ArrayList<String>();
        for (String s : options) {
           for (String base : previousResults) {
               results.add(s + base);
           }
        }
    }
    return results;
    

    This works (I hope!) similarly to the recursive method – at each iteration it “saves” the current progress (i.e. the result for n-1) to previousResults, then just iterates over the options in turn, to get the result of prepending them to the previous results.

    It would be interesting to see the effects of passing the recursive solution through any automatic recursion-to-iterative algorithms, and compare both the readability and performance to this hand-created one. This is left as an exercise for the reader.

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

Sidebar

Related Questions

I'm looking for an algorithm to find the common intersection points between 3 spheres.
I'm looking for an algorithm to find bounding box (max/min points) of a closed
I am looking for an algorithm that could find the two most distant elements
I am looking for a fast idea/algorithm letting me to find squares (as mark
I'm looking for a scoring algorithm but I really don't find what I'm looking
Using php, I am looking to find a set of unique combinations of a
I'm looking to find out if a particular algorithm already exists. I want to
I am looking for an algorithm that will take numbers or words and find
I am currently working on a algorithm that needs to find all equal occurrences
I need to create an algorithm that could find all the critical paths in

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.