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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:58:37+00:00 2026-05-23T16:58:37+00:00

Let’s say we have an array of age groups and an array of the

  • 0

Let’s say we have an array of age groups and an array of the number of people in each age group

For example:

Ages = ("1-13", "14-20", "21-30", "31-40", "41-50", "51+")
People = (1, 10, 21, 3, 2, 1)

I want to have an algorithm that combines these age groups with the following logic if there are fewer than 5 people in each group. The algorithm that I have so far does the following:

  1. Start from the last element (e.g., “51+”) can you combine it with the next group? (here “41-50”) if yes add the numbers 1+2 and combine their labels. So we get the following

    Ages = ("1-13", "14-20", "21-30", "31-40", "41+")
    People = (1, 10, 21, 3, 3)
    
  2. Take the last one again (here is “41+”). Can you combine it with the next group (31-40)? the answer is yes so we get:

    Ages = ("1-13", "14-20", "21-30", "31+")
    People = (1, 10, 21, 6)
    
  3. since the group 31+ now has 6 members we cannot collapse it into the next group.

  4. we cannot collapse “21-30” into the next one “14-20” either

  5. “14-20” also has 10 people (>5) so we don’t do anything on this either

  6. for the first one (“1-13”) since we have only one person and it is the last group we combine it with the next group “14-20” and get the following

    Ages = ("1-20", "21-30", "31+")
    People = (11, 21, 6)
    

I have an implementation of this algorithm that uses many flags to keep track of whether or not any data is changed and it makes a number of passes on the two arrays to finish this task.

My question is if you know any efficient way of doing the same thing? any data structure that can help? any algorithm that can help me do the same thing without doing too much bookkeeping would be great.

Update:
A radical example would be (5,1,5)

in the first pass it becomes (5,6) [collapsing the one on the right into the one in the middle]

then we have (5,6). We cannot touch 6 since it is larger than our threshold:5. so we go to the next one (which is element on the very left 5) since it is less than or equal to 5 and since it is the last one on the left we group it with the one on its right. so we finally get (11)

  • 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-23T16:58:37+00:00Added an answer on May 23, 2026 at 4:58 pm

    Here is my scala approach.
    We start with two lists:

    val people = List (1, 10, 21, 3, 2, 1)
    val ages = List ("1-13", "14-20", "21-30", "31-40", "41-50", "51+")
    

    and combine them to a kind of mapping:

    val agegroup = ages.zip (people)
    

    define a method to merge two Strings, describing an (open ended) interval. The first parameter is, if any, the one with the + in “51+”.

    /**
       combine age-strings 
       a+  b-c => b+
       a-b c-d => c-b
    */
    def merge (xs: String, ys: String) = {
      val xab = xs.split ("[+-]")
      val yab = ys.split ("-")
      if (xs.contains ("+")) yab(0) + "+" else 
      yab (0) +  "-" + xab (1)
    }    
    

    Here is the real work:

    /**
       reverse the list, combine groups < threshold. 
    */
    def remap (map: List [(String, Int)], threshold : Int) = {
    
      def remap (mappings: List [(String, Int)]) : List [(String, Int)] = mappings match {
        case           Nil =>      Nil 
        case x ::      Nil => x :: Nil 
        case x :: y :: xs  => if (x._2 > threshold) x :: remap (y :: xs) else 
          remap ((merge (x._1, y._1), x._2 + y._2) :: xs) }
    
      val nearly = (remap (map.reverse)).reverse
    
      // check for first element 
      if (! nearly.isEmpty && nearly.length > 1 && nearly (0)._2 < threshold) {
        val a = nearly (0)
        val b = nearly (1) 
        val rest = nearly.tail.tail 
        (merge (b._1, a._1), a._2 + b._2) :: rest
      } else nearly
    }
    

    and invocation

    println (remap (agegroup, 5))
    

    with result:

    scala> println (remap (agegroup, 5))
    List((1-20,11), (21-30,21), (31+,6))
    

    The result is a list of pairs, age-group and membercount.

    I guess the main part is easy to understand: There are 3 basic cases: an empty list, which can’t be grouped, a list of one group, which is the solution itself, and more than one element.

    If the first element (I reverse the list in the beginning, to start with the end) is bigger than 5 (6, whatever), yield it, and procede with the rest – if not, combine it with the second, and take this combined element and call it with the rest in a recursive way.

    If 2 elements get combined, the merge-method for the strings is called.

    The map is remapped, after reverting it, and the result reverted again. Now the first element has to be inspected and eventually combined.

    We’re done.

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

Sidebar

Related Questions

Let's say I have a drive such as C:\ , and I want to
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say you have a class library project that has any number of supplemental
Let's say I have a javascript array with a bunch of elements (anywhere from
Let's say for example i have URL containing the following percent encoded character :
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and

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.