Number of Employees Who Met the Target — Problem & Solution
Number of Employees Who Met the Target solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.
Number of Employees Who Met the Target is an easy coding problem on arrays, asked in Amazon, TCS and Capgemini interviews.
- Difficulty: Easy
- Topics: Arrays
- Asked at: Amazon, TCS, Capgemini
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
A company has n employees. hours[i] is the number of hours the i-th employee worked, and every employee is expected to work at least target hours.
Return how many employees met or exceeded the target.
Example 1
Input: hours = [0,1,2,3,4], target = 2
Output: 3
Explanation: Employees 2, 3 and 4 worked at least 2 hours.
Example 2
Input: hours = [5,1,4,2,2], target = 6
Output: 0
Example 3
Input: hours = [10], target = 0
Output: 1
Constraints
1 <= hours.length <= 500 <= hours[i], target <= 100000
Hints for Number of Employees Who Met the Target
Hint 1
One pass with a counter is all this needs.
Hint 2
The condition is "at least", so use
>=and not>.Hint 3
A target of 0 is met by everyone.
How to solve Number of Employees Who Met the Target
A direct count over an independent per-element predicate.
Approach
- Walk
hours. - Increment a counter whenever
hours[i] >= target. - Return the counter.
Why it works
Each employee's status depends only on their own hours, so the total is a plain sum of independent tests.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- Using a strict
>excludes employees who worked exactly the target. target = 0must count every employee, including those with 0 hours.
Reference solution
Python
from typing import List
def numberOfEmployeesWhoMetTarget(hours: List[int], target: int) -> int:
return sum(1 for h in hours if h >= target)Java
public static int numberOfEmployeesWhoMetTarget(int[] hours, int target) {
int count = 0;
for (int h : hours) {
if (h >= target) count++;
}
return count;
}C++
int numberOfEmployeesWhoMetTarget(vector<int>& hours, int target) {
int count = 0;
for (int h : hours) {
if (h >= target) count++;
}
return count;
}JavaScript
var numberOfEmployeesWhoMetTarget = function(hours, target) {
let count = 0;
for (let i = 0; i < hours.length; i++) {
if (hours[i] >= target) count++;
}
return count;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays