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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:01:16+00:00 2026-05-28T20:01:16+00:00

So I need an algorithm to generate all permutations of a list of numbers

  • 0

So I need an algorithm to generate all permutations of a list of numbers excluding cyclic rotations (e.g. [1,2,3] == [2,3,1] == [3,1,2]).

When there is at least 1 unique number in the sequence it is fairly straight forward, take out that unique number, generate all permutations of the remaining numbers (but with a small modification to the ‘standard’ permutations algorithm) and add the unique number to the front.

For generating the permutations I’ve found that it’s necessary to change the permutations code to:

def permutations(done, options)
    permuts = []
    seen = []
    for each o in options
        if o not in seen
            seen.add(o)
            permuts += permutations(done+o, options.remove(o))
    return permuts

Only using each unique number in options once means that you don’t get 322 twice.

This algorithm still outputs rotations when there are no unique elements, e.g. for [1,1,2,2] it would output [1,1,2,2], [1,2,2,1] and [1,2,1,2] and the first two are cyclic rotations.

So is there an efficient algorithm that would allow me to generate all the permutations without having to go through afterwards to remove cyclic rotations?

If not, what would be the most efficient way to remove cyclic rotations?

NOTE: this is not using Python, but rather C++.

  • 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-28T20:01:18+00:00Added an answer on May 28, 2026 at 8:01 pm

    Think about testing each of the permutations you output, looking for a cyclic rotation that’s “lexically” earlier than the one you’ve got. If there is one, don’t return it – it will have been enumerated somewhere else.

    Choosing a “unique” first element, if one exists, helps you optimize. You know if you fix the first element, and it’s unique, then you can’t possibly have duplicated it with a rotation. On the other hand, if there’s no such unique element, just choose the one that occurs the least. That way you only need to check for cyclic rotations that have that first element. (Example, when you generate [1,2,2,1] – you only need to check [1,1,2,2], not [2,2,1,1] or [2,1,1,2]).


    OK, pseudocode… clearly O(n!), and I’m convinced there’s no smarter way, since the case “all symbols different” obviously has to return (n-1)! elements.

    // generate all permutations with count[0] 0's, count[1] 1's...
    def permutations(count[])
        if(count[] all zero)
            return just the empty permutation;
        else
            perms = [];
            for all i with count[i] not zero
                r = permutations(copy of count[] with element i decreased);
                perms += i prefixed on every element of r
            return perms;
    
    // generate all noncyclic permutations with count[0] 0's, count[1] 1's...
    def noncyclic(count[])
        choose f to be the index with smallest count[f];
        perms = permutations(copy of count[] with element f decreased);
        if (count[f] is 1)
            return perms;
        else
            noncyclic = [];
            for each perm in perms
                val = perm as a value in base(count.length);
                for each occurence of f in perm
                    test = perm rotated so that f is first
                    tval = test as a value in base(count.length);
                    if (tval < val) continue to next perm;
                if not skipped add perm to noncyclic;
            return noncyclic;
    
    // return all noncyclic perms of the given symbols
    def main(symbols[])
        dictionary = array of all distinct items in symbols;
        count = array of counts, count[i] = count of dictionary[i] in symbols
        nc = noncyclic(count);
        return (elements of nc translated back to symbols with the dictionary)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Generate list of all possible permutations of a string I need to
I need an algorithm which given a list L and a number N ,
I need an efficient algorithm to generate all the subtrees expanded from the root
I need an algorithm which generates all possible combination of a set number and
I need to generate all permutation of a string with selecting some of the
I need to generate a list of modified files since last list generation on
Is there any way to randomly generate a set of positive numbers such that
I need to generate unique non-sequential alphanumeric string of exactly length 6 (not less,
I have an array / list of numbers. Each number has a certain priority
I have the following need (in python): generate all possible tuples of length 12

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.