Skip to main content

Number of Employees Who Met the Target - Solution & Explanation

EasyArray11 min readAsked at: Amazon, Microsoft
Practice this problem

Problem Statement

There are n employees in a company, numbered from 0 to n - 1. Each employee i has worked for hours[i] hours in the company.

The company requires each employee to work for at least target hours.

You are given a 0-indexed array of non-negative integers hours of length n and a non-negative integer target.

Return the integer denoting the number of employees who worked at least target hours.

 

Example 1:

Input: hours = [0,1,2,3,4], target = 2
Output: 3
Explanation: The company wants each employee to work for at least 2 hours.
- Employee 0 worked for 0 hours and didn't meet the target.
- Employee 1 worked for 1 hours and didn't meet the target.
- Employee 2 worked for 2 hours and met the target.
- Employee 3 worked for 3 hours and met the target.
- Employee 4 worked for 4 hours and met the target.
There are 3 employees who met the target.

Example 2:

Input: hours = [5,1,4,2,2], target = 6
Output: 0
Explanation: The company wants each employee to work for at least 6 hours.
There are 0 employees who met the target.

 

Constraints:

  • 1 <= n == hours.length <= 50
  • 0 <= hours[i], target <= 105

Approach Overview

Problem Overview: You receive an integer array hours where each value represents the number of hours an employee worked. Given a target value, count how many employees worked at least that many hours.

Approach 1: Iterative Counting (O(n) time, O(1) space)

The most direct solution is a single pass through the array. Iterate over each value in hours and check whether it is greater than or equal to target. If the condition is true, increment a counter. This works because the problem only requires counting valid elements, not storing them or modifying the array.

The key idea is simple conditional evaluation during traversal. Every element is checked exactly once, so the runtime grows linearly with the number of employees. No extra data structures are required, which keeps memory usage constant. This pattern appears frequently in array problems where you filter elements based on a condition.

This approach is typically what interviewers expect for an easy counting problem. It demonstrates that you can recognize when a straightforward linear scan is sufficient instead of overengineering a solution.

Approach 2: Functional Filtering (O(n) time, O(n) space)

Languages like Python and JavaScript support functional-style operations such as filter. Instead of manually managing a counter, you filter the array to keep only elements that satisfy hours[i] >= target, then compute the length of the filtered result.

The logic is identical to the iterative solution, but expressed declaratively. For example, Python can use len(list(filter(...))), while JavaScript can use hours.filter(h => h >= target).length. These constructs rely on built-in iteration internally.

The tradeoff is memory usage. The filter operation creates a temporary list containing all qualifying elements, which increases space complexity to O(n). Despite this, many developers prefer the readability and conciseness of functional style. It commonly appears in problems involving array processing and functional programming patterns.

Recommended for interviews: The iterative counting approach. It runs in O(n) time with O(1) extra space and clearly demonstrates control over loops and conditional checks. Showing the functional version afterward can highlight familiarity with modern language features, but the manual iteration proves stronger algorithmic fundamentals.

Approach 1: Iterative Approach

In this approach, we iterate through the list of hours and count how many employees met the target. For each employee, we check if the number of hours they worked is at least the target. If it is, we increment the counter. This straightforward approach gives us the result directly.

In this C solution, we define a function countEmployees() that iterates through the provided hours array, checking each value against target. If the condition hours[i] >= target is met, we increment the count. The total count is returned, representing the number of employees who met or exceeded the target.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the number of employees.
Space Complexity: O(1), as no extra space is used beyond a few variables.

Try this approach in the editor →

Approach 2: Functional Programming Approach with Filtering

This approach leverages the capabilities of functional programming languages by using filtering, which applies a condition to each element of the list and retains only those that satisfy the condition (i.e., hours worked are at least the target). We then return the count of the filtered list as the result.

Using a generator expression, this Python approach filters the list of hours based on whether each hour meets or exceeds the target. The sum function counts the number of true conditions, equating to the number of employees who met the criteria.

Code

Python

JavaScript

Complexity

Time Complexity: O(n), iterating over the list of employees.
Space Complexity: O(1), with only a temporary generator expression used.

Try this approach in the editor →

Approach 3: Iteration and Counting

We can iterate through the array hours. For each employee, if their working hours x is greater than or equal to target, then we increment the counter ans by one.

After the iteration, we return the answer.

The time complexity is O(n), where n is the length of the array hours. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Iterative Approach

Time Complexity: O(n), where n is the number of employees.
Space Complexity: O(1), as no extra space is used beyond a few variables.

Functional Programming Approach with Filtering

Time Complexity: O(n), iterating over the list of employees.
Space Complexity: O(1), with only a temporary generator expression used.

Iteration and Counting—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative CountingO(n)O(1)Best general solution. Minimal memory usage and expected in interviews.
Functional FilteringO(n)O(n)When using expressive functional features in Python or JavaScript and readability is preferred.

Video Solution

Leetcode | 2798. Number of Employees Who Met the Target | Easy | Java Solution • Developer Docs • 695 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Number of Employees Who Met the Target easy or hard?
Number of Employees Who Met the Target is categorized as an Easy problem. It focuses on basic array traversal and conditional checks, making it suitable for beginners practicing simple counting patterns in algorithms.
Number of Employees Who Met the Target Python/Java solution
Both Python and Java implementations follow the same idea: iterate through the array and count values greater than or equal to the target. Python may also use a filter expression, while Java typically uses a loop with a counter variable. Both approaches run in O(n) time.
How to solve Number of Employees Who Met the Target in O(n)?
Iterate through the hours array and compare each value with the target. Maintain a counter that increments whenever hours[i] >= target. After scanning the entire array, return the counter. This requires one linear pass and constant extra memory.
What is the best approach for Number of Employees Who Met the Target?
The best approach is a single-pass iterative count. Traverse the hours array and increment a counter whenever hours[i] >= target. This solution runs in O(n) time and O(1) space, which is optimal because every element must be inspected at least once.
Is Number of Employees Who Met the Target asked at Google/Amazon/Meta?
Problems of this style frequently appear in screening rounds at large tech companies because they test basic array traversal and conditional logic. While the exact problem may not always appear, similar counting and filtering array problems are common in interviews at companies like Amazon and Google.
What data structure is used in Number of Employees Who Met the Target?
The primary data structure is an array (or list). The algorithm simply scans the array and counts elements that satisfy a condition, so no additional structures like hash maps or stacks are required.
What is the time complexity of Number of Employees Who Met the Target?
The time complexity is O(n), where n is the number of employees in the hours array. Each element is checked exactly once against the target value. The optimal iterative solution also uses O(1) additional space.

Ready to solve this problem?

Practice Number of Employees Who Met the Target with our built-in code editor and test cases.

Practice on FleetCode