Number of Employees Who Met the Target - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: hours = [0,1,2,3,4], target = 2
Output: 3
💡 Note: Employees with indices 2, 3, and 4 worked at least 2 hours: hours[2] = 2, hours[3] = 3, hours[4] = 4
Example 2 — All Meet Target
$ Input: hours = [5,1,4,2,2], target = 1
Output: 5
💡 Note: All employees worked at least 1 hour, so all 5 employees meet the target
Example 3 — None Meet Target
$ Input: hours = [1,2,3], target = 5
Output: 0
💡 Note: No employee worked 5 or more hours, so 0 employees meet the target

Constraints

  • 1 ≤ n ≤ 50
  • 0 ≤ hours[i], target ≤ 105

Visualization

Tap to expand
Number of Employees Who Met the Target INPUT hours array: 0 idx 0 1 idx 1 2 idx 2 3 idx 3 4 idx 4 target = 2 Met target (>=2) Below target hours=[0,1,2,3,4], target=2 ALGORITHM STEPS 1 Use filter function Filter hours >= target 2 Check each element 0>=2? NO, 1>=2? NO... 3 Collect passing values 2>=2? YES, 3>=2? YES... 4 Count filtered array Return length of result filter(h => h >= target) Filtered: [2, 3, 4] Length = 3 hours.filter(h=>h>=t).length FINAL RESULT Employees who met target: 2h Emp 2 3h Emp 3 4h Emp 4 Output: 3 3 employees worked at least 2 hours return 3 Key Insight: The functional approach uses filter() to create a new array containing only elements that pass the condition (hours >= target), then returns the length of that filtered array. Time Complexity: O(n), Space: O(n) TutorialsPoint - Number of Employees Who Met the Target | Functional Approach
Asked in
Google 25 Amazon 20
25.0K Views
Medium Frequency
~5 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen