Click here to Skip to main content
14,662,597 members
Rate this:
Please Sign up or sign in to vote.
See more:
**QUESTION**

In a museum, the time when a customer comes in and gets out is registered. Therefore, there is a
journal, where you can get N intervals of time, where the value shows time when customer came
in, the second number shows time when customer got out of a museum.
You have to write a program, that calculates the maximum number of customers who were in a
museum at the same time

Sample input: 

6

 | 9:00 - 10:07 |

 | 10:20 - 11:35 |

 | 12:00 - 17:00 |

 | 11:00 - 11:30 |

 | 11:20 - 12:30 | 

 | 11:30 - 18:15 |

It shows the time when customers get in and out.

Sample output: 
4

So guys I need help with this one. I think this question can be done with for loop. 

Thanks!


What I have tried:

<pre lang="java"><pre>
     double in[] = {9, 10.20, 11, 11.20, 11.30, 12};
     double out[] = {10.07, 11.35, 11.30, 12.30, 18.15, 17};

     int total = 0;

     for(int i = 0; i< in.length; i++){
         for(int j = 0; j < out.length; j++){
             //not sure what to do here
          }
         }

     }
      System.out.println(total);
Posted
Updated 11-Oct-20 23:01pm

Rate this:
Please Sign up or sign in to vote.

Solution 1

Do not use double types to represent times as they are prone to inaccuracies. Convert each time to minutes past midnight. it is then a simple matter to count the number of people present at any time.
   
Comments
Rockin Retro 12-Oct-20 5:20am
   
I've converted all of them. But still I can't do it...
Richard MacCutchan 12-Oct-20 5:37am
   
Write the values out on paper, look at the different values and think about how you would find where most people are present. This exercise is largely about analysing and solving a logical problem; writing the code is the easy part.
Rockin Retro 12-Oct-20 6:08am
   
I've drawn a table. I have a line at the top that shows minutes. And below that, I have the lines that are showing each person's spending time in the museum. By this table, I can clearly see the maximum number of people who were there at the same time.
Richard MacCutchan 12-Oct-20 6:42am
   
So now you have to find a set of rules that will allow you to do it automatically.
Rockin Retro 12-Oct-20 7:23am
   
So I found a way called Sets intersection. But I'm not sure if it is the right way cause it does not work.
Rockin Retro 12-Oct-20 8:32am
   
Also, I have to submit it in 5 hours. Could you please express what you mean so I can do it asap pls?
Richard MacCutchan 12-Oct-20 8:40am
   
I cannot think what more I could say. This is a fairly straightforward problem. You need to find the point in time when the most people are inside. Start at the earliest time and step through the set of times. For each start time add 1 to the number of people inside. Keep a note of the time and the maximum number of people. For each end time subtract 1 from the number of people inside. When you have processed all the time values you should have a start and end time where the maximum number were present.
Rate this:
Please Sign up or sign in to vote.

Solution 2

We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100