Code Logo

Count of Elements Less Than Target

Published at05 Jan 2026
Easy 34 views
Like29

In Count of Elements Less Than Target, you are given an array of numbers and one target value. Your task is to count how many numbers in the array are strictly smaller than that target.

The word strictly matters here. A number should be counted only if it is less than the target, not equal to it. So if the target is 4, then 1 and 2 count, but 4 itself does not.

For example, if nums = [1,2,4,4,7] and target = 4, the answer is 2 because only 1 and 2 are smaller than 4. If nums = [3,3,3] and target = 3, the answer is 0 because none of the values are strictly less. If the array is empty, the answer is also 0.

So the task is to scan the array, test each value against the target, and count only the ones that are smaller.

Example Input & Output

Example 1
Input
nums = [], target = 10
Output
0
Explanation

The list is empty, so the count is 0.

Example 2
Input
nums = [1, 2, 4, 4, 7], target = 4
Output
2
Explanation

Only 1 and 2 are strictly less than 4, so the count is 2.

Example 3
Input
nums = [3, 3, 3], target = 3
Output
0
Explanation

No element is strictly smaller than 3.

Algorithm Flow

Recommendation Algorithm Flow for Count of Elements Less Than Target
Recommendation Algorithm Flow for Count of Elements Less Than Target

Solution Approach

The simplest way to solve this problem is to scan the array once and keep a counter of how many elements are strictly less than the target. You do not need sorting, binary search, or any extra data structure unless the problem explicitly guarantees the array is sorted and asks for an optimized search. Here, a direct count is the clearest match for the requirement.

The important part is the comparison itself. The condition should be num < target, not <=. That is what makes the answer correct for cases where values are equal to the target. In the example [1, 2, 4, 4, 7] with target 4, both 4 values must be excluded, because the problem says strictly less than.

In JavaScript, the core logic can be written like this:

let count = 0;
for (const num of nums) {
  if (num < target) {
    count++;
  }
}
return count;

This does exactly what the problem asks and nothing extra. Each element is checked once. If it is smaller than the target, increase the counter. Otherwise, ignore it and move on.

This approach also handles the edge cases naturally. If the array is empty, the loop never runs and the answer stays 0. If all values are greater than or equal to the target, the answer also stays 0. If every value is smaller than the target, then the answer becomes the full length of the array.

You could also write this using a filter expression, but the explicit loop often reads more clearly in explanation form because it shows the counting process step by step. The time complexity is O(n), since you inspect each value once, and the space complexity is O(1), since you only store a single counter.

So the full strategy is: initialize a counter, scan the array from left to right, increment the counter whenever a value is strictly smaller than the target, and return that final count.

Best Answers

java
import java.util.List;
class Solution {
    public int count_less_than(Object nums, Object target) {
        int t = (int) target;
        int count = 0;
        if (nums instanceof int[]) {
            for (int n : (int[])nums) {
                if (n < t) count++;
            }
        } else if (nums instanceof List) {
            for (Object n : (List)nums) {
                if ((int)n < t) count++;
            }
        }
        return count;
    }
}