Count Low Stock Items
Given an array of inventory quantities, count how many items have a quantity less than 10 (low stock). Return the number of low-stock items.
For example, quantities [15, 3, 20, 8, 12] has 2 low-stock items (3 and 8). All high stock [100, 50] has 0. All low stock [5, 2, 9] has 3. An empty array returns 0.
Threshold-based counting is used in inventory management (reorder alerts), quality control (defect thresholds), and monitoring systems (alert thresholds). It teaches comparison operators and conditional counting.
The solution initializes a counter, loops through each quantity, and increments when the value is below the threshold. Return the total after the loop.
Edge cases include an empty array (return 0), quantities exactly at the threshold (10 is not low), and negative quantities (which are below any positive threshold).
Example Input & Output
Only 3 and 2 are below the threshold.
None of the items are below the threshold.
With no items in the list, the low-stock count stays 0.
Algorithm Flow
Solution Approach
Iterate through the array and count elements less than the threshold of 10.
Initialize count to 0. For each quantity value, check if it is strictly less than 10. If so, increment the counter. After processing all items, return count. Values equal to exactly 10 are not counted as low stock since they meet the minimum threshold.
Time complexity is O(n), space complexity is O(1).
Best Answers
program count_low_stock_items
dictionary
stock: array[1..100] of integer
threshold, count, i, n: integer
algorithm
input(stock, threshold)
count <- 0
n <- stock.length
for i <- 0 to n - 1 do
if stock[i] < threshold then
count <- count + 1
endif
endfor
output(count)
endprogramComments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
