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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:27:21+00:00 2026-05-20T22:27:21+00:00

I have a number of items in an array, each one associated to a

  • 0

I have a number of items in an array, each one associated to a certain weight. There is a business rule saying that no two adjacent items cannot have a total weight of more than a certain value, let’s say 100 for simplicity.

For example, the following is OK:

[40,60,40,50]

But not this one (since 50+60 = 110)

[50,60,40] 

I am trying to implement an algorithm that will rearrange the items (if possible) so that the business rule is fulfilled. For example, the second one could be rearranged to
[60,40,50] or [50,40,60]

The algorithm should also tres to minimize the number of moved items, i.e. the first solution above is most appropriate since the “sub permutation” [60,40] is maintained.

I’m not looking for a complete answer or code examples, but would be happy if someone could point out some algorithms or categories of algorithms for this purpose. It would feel much better to rely on an existing and proved algorithm than some home-made stuff.

Note: In reality, the number of items is very large so testing all different permutations is NOT an option.

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

    Nice greedy solution: for the first place take maximum number. For each next place take maximum from untaken numbers before that satisfy your condition. If you place all numbers – you have found a solution. Otherwise the solution doesn’t exist, why – it’s an exercise for you.

    My proof: imagine a solution exists. Show, that my algorithm will find it. Let’s a_1, …, a_n – any solution. Let a_i – maximum element. Then a_i, a_{i-1}, …, a_1, a_{i+1}, a_{i+2}, …, a_n is a solution too, because a_1 <= a_i, a_1 + a_{i+1} <= a_i + a_{i+1}, so (a_i, a_{i+1}) is a good pair. Next, let a_1, …, a_j is element according to my solution. Show, that a_{j+1} can be element, as my solution suppose to. Let a_i – maximum from a_{j+1}, .., a_n. Then a_1, …, a_j, a_i, a_{i-1}, …, a{j+1}, a_{i+1}, …, a_n is a solution too. It shows that algo always find solution.

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

Sidebar

Related Questions

I have a couple of big arrays with numbers as items, each one has
I have a number of code value tables that contain a code and a
I have a number of tables that use the trigger/sequence column to simulate auto_increment
I have a number of RGBA pixels, each of them has an alpha component.
We have a number of projects that use the same and/or similar package names.
I need to have two UITableViews on one UIView. I can make it work
I have lists of variable length where each item can be one of four
In my app, I have a Spinner , that can be filled with two
I'm working on a project were we have number (5 at the moment) of
I have a number of macros written for Visual Studio 2005, but they 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.