Here, we will see **Program to find The Employee That Worked on the Longest Task** with code and algorithm.

You are given nÂ employees, each with a unique id fromÂ `0`

Â toÂ `n - 1`

. You are given a 2D integer arrayÂ `logs`

Â whereÂ `logs[i] = [id`

Â where:_{i}, leaveTime_{i}]

`id`

Â is the id of the employee that worked on theÂ_{i}`i`

Â task^{th}`leaveTime`

Â is the time at which the employee finished theÂ_{i}`i`

Â task. All the valuesÂ^{th}`leaveTime`

Â areÂ_{i}**unique**.

Note that theÂ `i`

Â task starts the moment right after theÂ ^{th}`(i - 1)`

Â task ends, and theÂ ^{th}`0`

Â task starts at timeÂ ^{th}`0`

.

You have to returnÂ *the id of the employee that worked the task with the longest time.*Â If there is a tie between two or more employees, return*Â theÂ smallestÂ id among them*.

A customer’sÂ **wealth**Â is the amount of money they have in all their bank accounts. The richest customer is the customer that has the maximumÂ **wealth**.

**Example:**

1)Input:n = 11, logs = [[1,4],[3,6],[1,10],[2,16]]Output:2Explanation:Task 0 started at 1 and ended at 4 with 3 units of times. Task 1 started at 4 and ended at 6 with 2 units of times. Task 2 started at 6 and ended at 10 with 4 units of times. Task 3 started at 10 and ended at 16 with 6 units of times. The task with the longest time is task 3 and the employee with id 2 is the one that worked on it, so we return 2.2)Input:n = 26, logs = [[1,1],[3,7],[2,12],[7,17]]Output:3Explanation:Task 0 started at 0 and ended at 1 with 1 unit of times. Task 1 started at 1 and ended at 7 with 6 units of times. Task 2 started at 7 and ended at 12 with 5 units of times. Task 3 started at 12 and ended at 17 with 5 units of times. The tasks with the longest time is task 1. The employees that worked on it is 3, so we return 3.3)Input:n = 2, logs = [[0,10],[1,20]]Output:0Explanation:Task 0 started at 0 and ended at 10 with 10 units of times. Task 1 started at 10 and ended at 20 with 10 units of times. The tasks with the longest time are tasks 0 and 1. The employees that worked on them are 0 and 1, so we return the smallest id 0.

## Program to find The Employee That Worked on the Longest Task code in C++

**Code 1:**

#include <iostream> #include <vector> using namespace std; int hardestWorker(int n, vector<vector<int>>& logs) { int max = logs[0][1], id = logs[0][0]; for(int i = 1; i < logs.size(); i++) { if((logs[i][1] - logs[i-1][1]) > max) { max = logs[i][1] - logs[i-1][1]; id = logs[i][0]; } else if((logs[i][1] - logs[i-1][1]) == max) { if(logs[i][0] < id) { id = logs[i][0]; } } } return id; } int main() { int n = 10; vector<vector<int> > logs = { {1, 4}, {3,6}, {1,10}, {2,16} }; cout<<hardestWorker(n, logs); return 0; }

**Code 2:**

#include <iostream> #include <vector> using namespace std; int hardestWorker(int n, vector<vector<int>>& logs) { int start = 0; int res = -1; int time = -1; for (vector<int> & log : logs) { auto id = log[0]; auto end = log[1]; int duration = end-start; if (duration > time) { time = duration; res = id; } else if (duration == time && res > id) { res = id; } start = end; } return res; } int main() { int n = 10; vector<vector<int> > logs = { {1, 4}, {3,6}, {1,10}, {2,16} }; cout<<hardestWorker(n, logs); return 0; }

**Output:**

2

**To check more leetcode problem’s solution. Pls click given below link:**

https://www.techieindoor.com/category/leetcode/