# 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.

**Examples:**

Input:P = 4, K = 6, X = 3Output:5Explanation:Let the four persons be P_{1}, P_{2}, P_{3}, P_{4}

- P
_{1}starts at 0 and finishes at 6- P
_{2}starts at 3 and finishes at 9- P
_{3 }starts at 6 and finishes at 12- P
_{4}starts at 9 and finishes at 15So, when P

_{1}finishes, P_{2 }and P_{3}started performing their respective job, count for P_{1}= 2

when P_{2}finishes, P_{3 }and P_{4 }started performing their respective job, count for P_{2}= 2

when P_{3}finishes, only P_{4}started performing the job, count for P_{3}= 1

when P_{4}finishes, there is no person who starts at this point, so count for P_{4 }= 0

Therefore, Total counts = (2 + 2 + 1 + 0) = 5

Input:P = 9, K = 72, X = 8Output:36

**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 P_{1}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:

## C++

`// C++ implementation for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the sum of count` `// of persons that are still` `// performing the job` `int` `countPersons(` `int` `P, ` `int` `K, ` `int` `X)` `{` ` ` `// Find the minimum of total persons` ` ` `// excluding the first one and K/X` ` ` `int` `a = min(P - 1, K / X);` ` ` `// If a is equal to 0, return 0` ` ` `if` `(a == 0) {` ` ` `return` `0;` ` ` `}` ` ` `// Else calculate the total sum of` ` ` `// count by the making a formula` ` ` `int` `ans = max(0,` ` ` `a * (a - 1) / 2)` ` ` `+ a * (P - a);` ` ` `// Return the sum of count` ` ` `return` `ans;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `P = 22, K = 9, X = 1;` ` ` `cout << countPersons(P, K, X);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.util.*;` `public` `class` `GFG {` `// Function to find the sum of count` `// of persons that are still` `// performing the job` `static` `int` `countPersons(` `int` `P, ` `int` `K, ` `int` `X)` `{` ` ` `// Find the minimum of total persons` ` ` `// excluding the first one and K/X` ` ` `int` `a = Math.min(P - ` `1` `, K / X);` ` ` `// If a is equal to 0, return 0` ` ` `if` `(a == ` `0` `) {` ` ` `return` `0` `;` ` ` `}` ` ` `// Else calculate the total sum of` ` ` `// count by the making a formula` ` ` `int` `ans = Math.max(` `0` `,` ` ` `a * (a - ` `1` `) / ` `2` `)` ` ` `+ a * (P - a);` ` ` `// Return the sum of count` ` ` `return` `ans;` `}` `// Driver Code` `public` `static` `void` `main(String args[])` `{` ` ` `int` `P = ` `22` `, K = ` `9` `, X = ` `1` `;` ` ` `System.out.println(countPersons(P, K, X));` `}` `}` `// This code is contributed by Samim Hossain Mondal.` |

## Python3

`# Python program for the above approach` `# Function to find the sum of count` `# of persons that are still` `# performing the job` `def` `countPersons(P, K, X):` ` ` `# Find the minimum of total persons` ` ` `# excluding the first one and K/X` ` ` `a ` `=` `min` `(P ` `-` `1` `, K ` `/` `X)` ` ` `# If a is equal to 0, return 0` ` ` `if` `a ` `=` `=` `0` `:` ` ` `return` `0` ` ` `# Else calculate the total sum of` ` ` `# count by the making a formula` ` ` `ans ` `=` `max` `(` `0` `,` ` ` `a ` `*` `(a ` `-` `1` `) ` `/` `2` `)` ` ` `# Return the sum of count` ` ` `return` `ans ` `+` `a ` `*` `(P ` `-` `a)` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` `P ` `=` `22` ` ` `K ` `=` `9` ` ` `X ` `=` `1` ` ` `print` `(` `int` `(countPersons(P, K, X)))` `# This code is contributed by Potta Lokesh` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG {` `// Function to find the sum of count` `// of persons that are still` `// performing the job` `static` `int` `countPersons(` `int` `P, ` `int` `K, ` `int` `X)` `{` ` ` `// Find the minimum of total persons` ` ` `// excluding the first one and K/X` ` ` `int` `a = Math.Min(P - 1, K / X);` ` ` `// If a is equal to 0, return 0` ` ` `if` `(a == 0) {` ` ` `return` `0;` ` ` `}` ` ` `// Else calculate the total sum of` ` ` `// count by the making a formula` ` ` `int` `ans = Math.Max(0,` ` ` `a * (a - 1) / 2)` ` ` `+ a * (P - a);` ` ` `// Return the sum of count` ` ` `return` `ans;` `}` `// Driver Code` `public` `static` `void` `Main()` `{` ` ` `int` `P = 22, K = 9, X = 1;` ` ` `Console.Write(countPersons(P, K, X));` `}` `}` `// This code is contributed by Samim Hossain Mondal.` |

## Javascript

`<script>` `// Javascript implementation for the above approach` `// Function to find the sum of count` `// of persons that are still` `// performing the job` `function` `countPersons(P, K, X)` `{` ` ` `// Find the minimum of total persons` ` ` `// excluding the first one and K/X` ` ` `let a = Math.min(P - 1, K / X);` ` ` `// If a is equal to 0, return 0` ` ` `if` `(a == 0) {` ` ` `return` `0;` ` ` `}` ` ` `// Else calculate the total sum of` ` ` `// count by the making a formula` ` ` `let ans = Math.max(0,` ` ` `a * (a - 1) / 2)` ` ` `+ a * (P - a);` ` ` `// Return the sum of count` ` ` `return` `ans;` `}` `// Driver Code` `let P = 22, K = 9, X = 1;` `document.write(countPersons(P, K, X));` `// This code is contributed by Samim Hossain Mondal.` `</script>` |

**Output**

153

**Time Complexity: **O(1) **Auxiliary Space: **O(1)