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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:42:12+00:00 2026-05-21T03:42:12+00:00

I am trying to work out the best way to generate all possible permutations

  • 0

I am trying to work out the best way to generate all possible permutations for a sequence which is a fixed number of digits and each digit has a different alphabet.

I have a number of integer arrays and each one can have different length and when generating the permutations only the values of the array can occupy the position in the final results.

A specific example is an int array called conditions with the following data:

conditions1 = {1,2,3,4}
conditions2 = {1,2,3}
conditions3 = {1,2,3}
conditions4 = {1,2}
conditions5 = {1,2} 

and I want to create a 5 column table of all the possible permutations – this case 144 (4x3x3x2x2). Column 1 can only use the values from conditions1 and column 2 from conditions2, etc.

output would be :

1,1,1,1,1
1,1,1,1,2
1,1,1,2,1
1,1,1,2,2
1,1,2,1,1
.
.
through to 
4,3,3,2,2

It’s been too long since since I’ve done any of this stuff and most of the information I’ve found relates to permutations with the same alphabet for all fields. I can use that then run a test after removing all the permutations that have columns with invalid values but sounds inefficient.

I’d appreciate any help here.

Z.

  • 1 1 Answer
  • 3 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-21T03:42:13+00:00Added an answer on May 21, 2026 at 3:42 am

    Look ma, no recursion needed.

    Iterator<int[]> permutations(final int[]... conditions) {
      int productLengths = 1;
      for (int[] arr : conditions) { productLengths *= arr.length; }
      final int nPermutations = productLengths;
      return new Iterator<int[]>() {
        int index = 0;
        public boolean hasNext() { return index < nPermutations; }
        public int[] next() {
          if (index == nPermutations) { throw new NoSuchElementException(); }
          int[] out = new int[conditions.length];
          for (int i = out.length, x = index; --i >= 0;) {
            int[] arr = conditions[i];
            out[i] = arr[x % arr.length];
            x /= arr.length;
          }
          ++index;
          return out;
        }
        public void remove() { throw new UnsupportedOperationException(); }
      };
    }
    

    Wrapping it in an Iterable<int[]> will make it easier to use with a for (... : ...) loop. You can get rid of the array allocation by doing away with the iterator interface and just taking in as argument an array to fill.

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

Sidebar

Related Questions

I'm stuck trying to work out the best possible way to approach this. Basically,
I am trying to work out the best way of doing this but not
I've been trying to work out the 'best practices' way to manage file uploads
I'm trying to work out what the best method to passing a large number
I'm trying to work out the best way to assemble the navigation menu of
I am trying to work out the best way to stop double 'booking' in
I'm just trying to work out the best way to replicate a pretty useful
Im trying to work out the best way to approach the following problem with
I'm trying to work out the best way to setup our multi-module Apache Maven
I am trying to work out the best way of using a viewmodel 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.