Count Sold Tickets
Given an array of ticket sales where each element represents the number of tickets sold in a day, count how many days had ticket sales greater than zero. Return the total count of days with at least one ticket sold.
For example, sales [0, 5, 0, 3, 10] has 3 days with sales (5, 3, 10). Sales [0, 0, 0] has 0 days. Sales [1] has 1 day. An empty array returns 0.
Counting elements that satisfy a condition is a fundamental data aggregation pattern. It appears in inventory management (counting items below reorder threshold), attendance tracking (counting days with presence), and quality control (counting defective units).
The solution initializes a counter to 0, iterates through each element, and increments the counter whenever the element is greater than zero. After the loop, return the counter. This runs in O(n) time with O(1) space.
Edge cases include an empty array (return 0), an array where all values are zero (return 0), and an array where all values are positive (the count equals the array length).
Example Input & Output
An empty list means there are no sold tickets to count.
Three of the tickets are marked as sold.
If no ticket is sold yet, the count stays 0.
Algorithm Flow

Solution Approach
Iterate through the array and count elements that are greater than zero.
Initialize count to 0. Loop through each ticket sale value. If the value is greater than 0, increment the counter. After processing all entries, return the count. The algorithm correctly handles empty arrays (the loop never runs, count stays 0).
Time complexity is O(n) where n is the number of days. Space complexity is O(1).
Best Answers
program count_sold_tickets
dictionary
status: array[1..100] of integer
sold_count, i, n: integer
algorithm
input(status)
sold_count <- 0
n <- status.length
for i <- 0 to n - 1 do
if status[i] = 1 then
sold_count <- sold_count + 1
endif
endfor
output(sold_count)
endprogramComments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
