We are given n-processes with their times in form of an array. We need to find the instant when a given p ends if the scheduling is round robin and time slice is 1-sec.

Examples:

```Input : arr[] = {3, 2, 4, 2}, p = 1
Output : Completion time = 6
Explanation : Snap of process for every second is as:
Time    |   Process Array
0       |   {3, 2, 4, 2}
1       |   {2, 2, 4, 2}
2       |   {2, 1, 4, 2}
3       |   {2, 1, 3, 2}
4       |   {2, 1, 3, 1}
5       |   {1, 1, 3, 1}
6       |   {1, 0, 3, 1}

Input : arr[] = {2, 4, 1, 3}, p = 2
Output :Completion time = 3
Explanation : Snap of process for every second is as:
Time    |   Process Array
0       |   {2, 4, 1, 3}
1       |   {1, 4, 1, 3}
2       |   {1, 3, 1, 3}
3       |   {1, 3, 0, 3}
```

Brute Force :The basic approach for solving this problem is to apply round algorithm with time slice 1. But the time complexity of that approach will be O(ΣAi) i.e. summation of all process’s time, which is quite high.

Efficient Approach: The idea is based on below observations.
1) All processes with CPU time less than arr[p] would complete before arr[p]. We simply need to add time of these processes.
2) We also need to add time of arr[p].
3) For every process x with CPU time more than arr[p], two cases arise :
…..(i) If x is on left of arr[p] (scheduled before arr[p]), then this process takes arr[p] time of CPU before p finishes.
…..(ii) If x is on right of arr[p] (scheduled after arr[p]), then this process takes arr[p]-1 time of CPU before p finishes.

Algorithm :

```time_req = 0;

// Add time for process on left of p
// (Scheduled before p in a round of
// 1 unit time slice)
for (int i=0; i<p; i++)
{
if (arr[i] < arr[p])
time_req += arr[i];
else
time_req += arr[p];
}

// step 2 : Add time of process p
time_req += arr[p];

// Add time for process on right
// of p (Scheduled after p in
// a round of 1 unit time slice)
for (int i=p+1; i<n; i++)
{
if (arr[i] < arr[p])
time_req += arr[i];
else
time_req += arr[p]-1;
}
```
```// Program to find end time of a process
// p in round robin scheduling with unit
// time slice.
#include <bits/stdc++.h>
using namespace std;

// Returns completion time of p.
int completionTime(int arr[], int n, int p) {

// Initialize result
int time_req = 0;

// Step 1 : Add time of processes on left
//  of p (Scheduled before p)
for (int i = 0; i < p; i++) {
if (arr[i] < arr[p])
time_req += arr[i];
else
time_req += arr[p];
}

// Step 2 : Add time of p
time_req += arr[p];

// Step 3 : Add time of processes on right
//  of p (Scheduled after p)
for (int i = p + 1; i < n; i++) {
if (arr[i] < arr[p])
time_req += arr[i];
else
time_req += arr[p] - 1;
}

return time_req;
}

// driver program
int main() {
int arr[] = {3, 5, 2, 7, 6, 1};
int n = sizeof(arr) / sizeof(arr[0]);
int p = 2;
cout << "Completion time = "
<< completionTime(arr, n, p);
return 0;
}
```

Output:

```Completion time = 9
```

Time Complexity : O(n)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.