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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:24:57+00:00 2026-06-04T09:24:57+00:00

We are implementing a density report for a call center. The result must be

  • 0

We are implementing a density report for a call center. The result must be displayed as table with a row per day showing the maximum number of simultaneously active calls during that day.

We are building the lib behind the UI. The contract specifies we receive the number of calls for that day and two arrays of integers, one with the start time and one with the end time of each call, so, for example:

For a given day just two calls are received: One goes from time 20 to 30 and the other one from 10 to 20. The maximum number simultaneously calls is 1.

On the other hand, for another day, also two calls are received, one from 10 to 45 and the other from 15 to 40 then the maximum number of simultaneously calls is 2.

The contract for the web service is this

public static int GetMaxDensity(int N, int[] X, int[] Y)

And the data looks like this (suppose 3 calls where received that day). First one from 10 to 25, second one from 12 to 30 and third one from 20 to 23.

N = 3, 
X = {10, 12, 20}
Y = {25, 30, 23}

And the return must be: 3.

I’ve implemented this solution:

public static int GetMaxDensity(int N, int[] X, int[] Y) 
{
  int result = 0;
  for (int i = 0; i < N; i++) 
  {
      int count = 0, t = X[i];
      for (int j = 0; j < N; j++) 
      {
        if (X[j] <= t && t < Y[j])
        count++;
      }
      result = Math.max(count, result);
   }
   return result;
}

And it works great when the number of calls is up to 1000 (weekends) but within work days the number is pretty big and the calculation takes so long (>5 minutes). I now the reason could be my solution is using two nested cycles but I don’t have pretty much experience with complex algorithms so my question is:

Given that I just need the maximum number of simultaneously calls (not the times nor the callers), which could be a faster way to perform this calculation if there is one.

  • 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-04T09:24:58+00:00Added an answer on June 4, 2026 at 9:24 am

    As N grows your time grows rapidly (N*N). A simple solution (if your times are in intervals of minutes past midnight) would be to create an array of 1440 ints that will contain the change in call counts for each minute through the day. Then you can loop just once from 0 to N-1, and for each element, adjust the count of the call count delta at that point in time by incrementing the value at the time the call starts, and decrementing at the time it ends. After that, just look through the counts to obtain the largest value. This should be much faster for larger values of N.

    Since 1440 is a constant (for the last step), and the inputs do not need to be sorted, this should have linear time complexity. This algorithm’s run time is not affected by the average call length.

    public static int GetMaxDensity(int N, int[] X, int[] Y) {
        int rangeStart = Integer.MAX_VALUE;
        int rangeEnd = Integer.MIN_VALUE;
        for(int i=0; i<N; i++) {
            if (X[i] < rangeStart) rangeStart = X[i];
            if (Y[i] > rangeEnd) rangeEnd = Y[i];
        } 
        int rangeSize = rangeEnd - rangeStart + 1;
        int[] histogram = new int[rangeSize];
        for (int t = 0; t < rangeSize; t++) histogram[t] = 0;
        for (int i = 0; i < N; i++) {
            histogram[X[i]-rangeStart]++;
            histogram[Y[i]-rangeStart]--;
        }
        int maxCount = 0;
        int count = 0;
        for (int t = 0; t < rangeSize; t++) {
            count += histogram[t];
            if (count > maxCount) maxCount = count;
        }
        return maxCount;        
    }
    

    For comparision, with N=50,000 and random call lengths between 1 and 40 minutes, the algorithm in the question used 29,043 milliseconds, and this algorithm used 8 milliseconds. I ran these tests in c#, but they should be comparable to what Java would produce.

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

Sidebar

Related Questions

The implementing-result-paging-in-hibernate-getting-total-number-of-rows question trigger another question for me, about some implementation concern : Now
While implementing XML file reading/writing in my application I saw that when I call
In implementing faceted search, if the number of options is 7 or less, I
Implementing 8bit ALU in VHDL with unsigned numbers only. When the result of the
Does implementing the Java Security Manager result in decreased performance?
Implementing the ScriptControlClass was extremely easy, unfortunately the side effects with the language implementation
implementing publishActivity in PHP using the REST API using this code: $activity = array(
Implementing a custom membership provider, there are certain properties such as MinRequiredPasswordLength that only
Implementing a simple Login screen using JSF and Spring and Hibernate. I have written
When implementing the Strategy Pattern, where does one put the code that determines which

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.