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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:04:22+00:00 2026-06-11T08:04:22+00:00

I have a list of elements, each one identified with a type, I need

  • 0

I have a list of elements, each one identified with a type, I need to reorder the list to maximize the minimum distance between elements of the same type.

The set is small (10 to 30 items), so performance is not really important.

There’s no limit about the quantity of items per type or quantity of types, the data can be considered random.

For example, if I have a list of:

  • 5 items of A
  • 3 items of B
  • 2 items of C
  • 2 items of D
  • 1 item of E
  • 1 item of F

I would like to produce something like:
A, B, C, A, D, F, B, A, E, C, A, D, B, A

  • A has at least 2 items between occurences
  • B has at least 4 items between occurences
  • C has 6 items between occurences
  • D has 6 items between occurences

Is there an algorithm to achieve this?

-Update-

After exchanging some comments, I came to a definition of a secondary goal:

  • main goal: maximize the minimum distance between elements of the same type, considering only the type(s) with less distance.
  • secondary goal: maximize the minimum distance between elements on every type. IE: if a combination increases the minimum distance of a certain type without decreasing other, then choose it.

-Update 2-

About the answers.
There were a lot of useful answers, although none is a solution for both goals, specially the second one which is tricky.

Some thoughts about the answers:

  • PengOne: Sounds good, although it doesn’t provide a concrete implementation, and not always leads to the best result according to the second goal.
  • Evgeny Kluev: Provides a concrete implementation to the main goal, but it doesn’t lead to the best result according to the secondary goal.
  • tobias_k: I liked the random approach, it doesn’t always lead to the best result, but it’s a good approximation and cost effective.

I tried a combination of Evgeny Kluev, backtracking, and tobias_k formula, but it needed too much time to get the result.

Finally, at least for my problem, I considered tobias_k to be the most adequate algorithm, for its simplicity and good results in a timely fashion. Probably, it could be improved using Simulated annealing.

  • 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-11T08:04:23+00:00Added an answer on June 11, 2026 at 8:04 am

    This sounded like an interesting problem, so I just gave it a try. Here’s my super-simplistic randomized approach, done in Python:

    def optimize(items, quality_function, stop=1000):
        no_improvement = 0
        best = 0
        while no_improvement < stop:
            i = random.randint(0, len(items)-1)
            j = random.randint(0, len(items)-1)
            copy = items[::]
            copy[i], copy[j] = copy[j], copy[i]
            q = quality_function(copy)
            if q > best:
                items, best = copy, q
                no_improvement = 0
            else:
                no_improvement += 1
        return items
    

    As already discussed in the comments, the really tricky part is the quality function, passed as a parameter to the optimizer. After some trying I came up with one that almost always yields optimal results. Thank to pmoleri, for pointing out how to make this a whole lot more efficient.

    def quality_maxmindist(items):
        s = 0
        for item in set(items):
            indcs = [i for i in range(len(items)) if items[i] == item]
            if len(indcs) > 1:
                s += sum(1./(indcs[i+1] - indcs[i]) for i in range(len(indcs)-1))
        return 1./s
    

    And here some random result:

    >>> print optimize(items, quality_maxmindist)
    ['A', 'B', 'C', 'A', 'D', 'E', 'A', 'B', 'F', 'C', 'A', 'D', 'B', 'A']
    

    Note that, passing another quality function, the same optimizer could be used for different list-rearrangement tasks, e.g. as a (rather silly) randomized sorter.

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

Sidebar

Related Questions

I have two lists with 8 list elements within each one. I would like
I have a list where elements are hidden for multiple reasons, each corresponding to
I have a list that contains many sub-lists of 3 elements each, like: my_list
I have a list of elements (integers) and what I need to do is
I have a list of li elements that I need to allow drag/drop into
Suppose I have a list, called elements , each of which does or does
I have a list of elements to move through, and one at a time
I have a list of type List<Element<Integer, Integer>> , where each element contains two
I have an unordered list and each list element contains a photo and a
I have a list of lists that looks like this: x[[state]][[year]] . Each element

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.