Average value of even numbers that are divisible by three

November 8, 2022

math-and-geometry

Problem URL: Average value of even numbers that are divisible by three

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)