Sum of count of persons still performing the job whenever a person finishes job
Given two integers P denoting the number of people and a positive integer K. Each person is assigned with the same job which takes exactly K hours to finish. A condition is given that all of them can start performing their jobs exactly in X hours of interval. Count the number of persons still performing the job whenever a prior person finishes his/her job. The task is to find the sum of such counts.
Input: P = 4, K = 6, X = 3
Explanation: Let the four persons be P1, P2, P3, P4
- P1 starts at 0 and finishes at 6
- P2 starts at 3 and finishes at 9
- P3 starts at 6 and finishes at 12
- P4 starts at 9 and finishes at 15
So, when P1 finishes, P2 and P3 started performing their respective job, count for P1 = 2
when P2 finishes, P3 and P4 started performing their respective job, count for P2 = 2
when P3 finishes, only P4 started performing the job, count for P3 = 1
when P4 finishes, there is no person who starts at this point, so count for P4 = 0
Therefore, Total counts = (2 + 2 + 1 + 0) = 5
Input: P = 9, K = 72, X = 8
Approach: The given problem can be solved by analyzing the problem with the concept of math. Follow the steps below to solve the problem:
- Find the minimum of total persons excluding the first one(because P1 always starts at 0) and K/X, store it in a variable say a.
- Check if a is equal to 0
- if yes, return 0.
- else, calculate the total sum of count by making mathematical formula.
- Return the final sum of count as the required answer.
Below is the implementation of the above approach:
Time Complexity: O(1)
Auxiliary Space: O(1)
Please Login to comment...