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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:06:37+00:00 2026-05-26T12:06:37+00:00

Given an array of N elements representing the permutation atoms, is there an algorithm

  • 0

Given an array of N elements representing the permutation atoms, is there an algorithm like that:

function getNthPermutation( $atoms, $permutation_index, $size )

where $atoms is the array of elements, $permutation_index is the index of the permutation and $size is the size of the permutation.

For instance:

$atoms = array( 'A', 'B', 'C' );
// getting third permutation of 2 elements
$perm = getNthPermutation( $atoms, 3, 2 );

echo implode( ', ', $perm )."\n";

Would print:

B, A

Without computing every permutation until $permutation_index ?

I heard something about factoradic permutations, but every implementation i’ve found gives as result a permutation with the same size of V, which is not my case.

Thanks.

  • 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-26T12:06:38+00:00Added an answer on May 26, 2026 at 12:06 pm

    As stated by RickyBobby, when considering the lexicographical order of permutations, you should use the factorial decomposition at your advantage.

    From a practical point of view, this is how I see it:

    • Perform a sort of Euclidian division, except you do it with factorial numbers, starting with (n-1)!, (n-2)!, and so on.
    • Keep the quotients in an array. The i-th quotient should be a number between 0 and n-i-1 inclusive, where i goes from 0 to n-1.
    • This array is your permutation. The problem is that each quotient does not care for previous values, so you need to adjust them. More explicitly, you need to increment every value as many times as there are previous values that are lower or equal.

    The following C code should give you an idea of how this works (n is the number of entries, and i is the index of the permutation):

    /**
     * @param n The number of entries
     * @param i The index of the permutation
     */
    void ithPermutation(const int n, int i)
    {
       int j, k = 0;
       int *fact = (int *)calloc(n, sizeof(int));
       int *perm = (int *)calloc(n, sizeof(int));
    
       // compute factorial numbers
       fact[k] = 1;
       while (++k < n)
          fact[k] = fact[k - 1] * k;
    
       // compute factorial code
       for (k = 0; k < n; ++k)
       {
          perm[k] = i / fact[n - 1 - k];
          i = i % fact[n - 1 - k];
       }
    
       // readjust values to obtain the permutation
       // start from the end and check if preceding values are lower
       for (k = n - 1; k > 0; --k)
          for (j = k - 1; j >= 0; --j)
             if (perm[j] <= perm[k])
                perm[k]++;
    
       // print permutation
       for (k = 0; k < n; ++k)
          printf("%d ", perm[k]);
       printf("\n");
    
       free(fact);
       free(perm);
    }
    

    For example, ithPermutation(10, 3628799) prints, as expected, the last permutation of ten elements:

    9 8 7 6 5 4 3 2 1 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an arbitrary array of size n , I'd like to reorganize the elements
Given an array. How can we find sum of elements in index interval (i,
Given an array with N elements. We know that one of those elements repeats
Suppose that you're given an array A of n distinct elements drawn from some
Given you have an array A[1..n] of size n, it contains elements from the
Given an array like {one two, three four five}, how'd you calculate the total
Consider the following problem. We are given an array of elements belonging to one
i have seen a few days ago such problem there is given two array
How to count array elements after the first occurrence of a given element? For
I came across the following question. Given an array of n elements and an

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.