Here’s another example that involves mean averages. Let’s say we’re building weather-forecasting software. To determine the temperature of a city, we take temperature readings from across many thermometers across the city, and we calculate the mean average of those temperatures.

We’d also like to display the temperatures in both Fahrenheit and Celsius, but our readings are initially only provided to us in Fahrenheit.

To get the average Celsius temperature, our algorithm does two things: first, it converts all the readings from Fahrenheit to Celsius. Then, it calculates the mean average of all the Celsius numbers.

Following is some Ruby code that accomplishes this. What is its Big O?

​  ​ def ​ ​ average_celsius ​(fahrenheit_readings)
​  
​  ​ # Collect Celsius numbers here: ​
​  celsius_numbers = []
​  
​  ​ # Convert each reading to Celsius and add to array: ​
​  fahrenheit_readings.​ each ​ ​ do ​ |fahrenheit_reading|
​  celsius_conversion = (fahrenheit_reading - 32) / 1.8
​  celsius_numbers.​ push ​(celsius_conversion)
​  ​ end ​
​  
​  ​ # Get sum of all Celsius numbers: ​
​  sum = 0.0
​  
​  celsius_numbers.​ each ​ ​ do ​ |celsius_number|
​  sum += celsius_number
​  ​ end ​
​  
​  ​ # Return mean average: ​
​  ​ return ​ sum / celsius_numbers.​ length ​
​  ​ end ​

First, we can say that N is the number of fahrenheit_readings passed into our method.

Inside the method, we run two loops. The first converts the readings to Celsius, and the second sums all the Celsius numbers. Since we have two loops that each iterate over all N elements, we have N + N, which is 2N (plus a few constant steps). Because Big O Notation drops the constants, this gets reduced to O(N).

Don’t get thrown off by the fact that in the earlier Word Builder example, two loops led to an efficiency of O(N 2 ). There, the loops were nested, which led to N steps multiplied by N steps. In our case, however, we simply have two loops, one after the other. This is N steps plus N steps (2N), which is a mere O(N).