Code Logo

Count Sold Tickets

Published at19 Apr 2026
Loops & Iteration Easy 6 views
Like0

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).

Learn about our pseudocode specification
Guide

Example Input & Output

Example 1
Input
status = []
Output
0
Explanation

An empty list means there are no sold tickets to count.

Example 2
Input
status = [1,1,0,1]
Output
3
Explanation

Three of the tickets are marked as sold.

Example 3
Input
status = [0,0,0]
Output
0
Explanation

If no ticket is sold yet, the count stays 0.

Algorithm Flow

Recommendation Algorithm Flow for Count Sold Tickets
Recommendation Algorithm Flow for Count Sold Tickets

Solution Approach

Iterate through the array and count elements that are greater than zero.

function countSales(sales)
  count = 0
  for each s in sales
    if s > 0 then count = count + 1
  return count

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

Pseudocode - Approach 1
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)
endprogram