math and geometry November 8, 2022

Average value of even numbers that are divisible by three

Time O(n) Space O(n) Open original problem

We will iterate over all elements and filter the numbers that are divisible by 2*3=6. Then we will calculate the average of the numbers. If there are no numbers that are divisible by 6, we will return 0.

class Solution:
    def averageValue(self, nums: List[int]) -> int:
        nums = [n for n in nums if n%(2*3)==0]
        return int(sum(nums)/len(nums)) if len(nums) > 0 else 0

Time complexity: O(n)
Space complexity: O(n)