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

  • Home
  • SEARCH
  • 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 654863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:32:26+00:00 2026-05-13T22:32:26+00:00

I need to calculate permutations iteratively. The method signature looks like: int[][] permute(int n)

  • 0

I need to calculate permutations iteratively. The method signature looks like:

int[][] permute(int n)

For n = 3 for example, the return value would be:

[[0,1,2],
 [0,2,1],
 [1,0,2],
 [1,2,0],
 [2,0,1],
 [2,1,0]]

How would you go about doing this iteratively in the most efficient way possible? I can do this recursively, but I’m interested in seeing lots of alternate ways to doing it iteratively.

  • 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-13T22:32:27+00:00Added an answer on May 13, 2026 at 10:32 pm

    see QuickPerm algorithm, it’s iterative : http://www.quickperm.org/

    Edit:

    Rewritten in Ruby for clarity:

    def permute_map(n)
      results = []
      a, p = (0...n).to_a, [0] * n
      i, j = 0, 0
      i = 1
      results << yield(a)
      while i < n
        if p[i] < i
          j = i % 2 * p[i] # If i is odd, then j = p[i], else j = 0
          a[j], a[i] = a[i], a[j] # Swap
          results << yield(a)
          p[i] += 1
          i = 1
        else
          p[i] = 0
          i += 1
        end
      end
      return results
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need to calculate IP => int just like in here: https://bitbucket.org/lorien/django-ipgeo/src/d19648c9b14f/ipgeo/models.py but in ruby.
I need to calculate date (year, month, day) which is (for example) 18 working
I need to calculate modulus with large number like : <?php $largenum = 95635000009453274121700;
I need to calculate iphone velocity in space, punch speed for example. My first
I need to calculate the median value in MySQL. I saw the solution here
I need to calculate precision and recall value in lucene and I use this
I need to calculate 4.0468564224e-33 value. But unfortunately I am getting a result of
I need to calculate a percentage from a cumulative value. The percent rate to
I need to calculate the screen width on orientationchange. I'm doing it like this:
I need to calculate 5-star ratings like the one on Amazon website. I have

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.