How do you calculate total duration of overlapping time-spans


How do you calculate total duration of overlapping time-spans



This code is good for a set of times sorted in ascending order only!



I was getting the wrong answer because the original set of times were given and expected to be calculated unsorted



I'm trying to write a piece of code that will calculate the total duration of a given set of task times, where w tasks can be worked on at any given time. If there are two tasks going on, and one of them finish, then another will start immediately.



To give some context, the times are in a sorted array as below;


int times = {2,2,3,4}; //short array for example

int n = sizeof(times) / sizeof(times[0]);
int w = 2; //number of tasks that can be done simultatenously
int total_time = 0;



The method I'm going with is to sum all the wth times in the array. So in this example,the total time should be the second and fourth values (2+4).For that I run a for loop.


int main()
{
for (int i = n-1; i >= 0; i -= w)
{
//std::cout << "At index " << i << " is value " << myarray[i] << endl;
total_time += myarray[i];

}
std::cout << total_time << endl;
std::cin.get();
}



If w = 3, then the result should be the same in this example



When w > n, then I just return the last value, since if all the tasks are done simultaneously, the duration will simply be the largest value in the array. I think my method is not working as it get the wrong results for some arrays.
Can anyone see where I'm going wrong?



edit: People have asked for an example where it goes wrong


int times{
125,
500,
1000,
2500,
5000,
10000,
15625,
25000,
50000,
62500,
78125,
78125,
100000,
125000,
156250,
500000,
781250,
5000000,
6250000,
6250000
};

w = 5;
int n = sizeof(times) / sizeof(times[0]);



For this the answer is 6515625. I'm getting 6473750. Either I'm missing something really obvious or the test case is wrong (Which I doubt)



The test case values were not sorted. Sorting ruins the answer





Can you show which array gives the wrong result and what the correct result should be?
– NathanOliver
Jun 29 at 17:41





edited question to include test case
– Ozymandias
Jun 29 at 17:46





Well you shouldn't get less than 6250000 as that is one of your timings.
– NathanOliver
Jun 29 at 17:51


6250000





if i do it by hand I also get 6473750.
– Klaus
Jun 29 at 17:51





Your method will indeed give 6473750 for this test case. So when you say that w tasks can be worked on simultaneously, does that also mean that all w tasks has to finish before a new task can start? Otherwise the answer here should be 6250000 from my understanding.
– super
Jun 29 at 18:26


w


w









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift