problemeasyalgorithmsleetcode-2455leetcode 2455leetcode2455

Average Value of Even Numbers That Are Divisible by Three

EasyUpdated: Aug 2, 2025
Practice on:

Problem

Given an integer array nums of positive integers, return the average value of all even integers that are divisible by 3 .

Note that the average of n elements is the sum of the n elements divided by n and rounded down to the nearest integer.

Examples

Example 1

    Input: nums = [1,3,6,10,12,15]
    Output: 9
    Explanation: 6 and 12 are even numbers that are divisible by 3. (6 + 12) / 2 = 9.

Example 2

Input: nums = [1,2,4,7,10]
Output: 0
Explanation: There is no single number that satisfies the requirement, so return 0.

Constraints

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 1000

Solution

Method 1 – Simple Iteration and Counting

Intuition

We want the average of numbers that are both even and divisible by 3. We can iterate through the array, sum such numbers, and count them. If none exist, return 0.

Approach

  1. Initialize s (sum) and cnt (count) to 0.
  2. For each number in the array:
    • If it is even and divisible by 3, add to s and increment cnt.
  3. If cnt is 0, return 0. Otherwise, return integer division of s by cnt.

Code

C++
class Solution {
public:
    int averageValue(vector<int>& nums) {
        int s = 0, cnt = 0;
        for (int n : nums) {
            if (n % 2 == 0 && n % 3 == 0) {
                s += n;
                cnt++;
            }
        }
        return cnt == 0 ? 0 : s / cnt;
    }
};
Go
type Solution struct{}

func (Solution) AverageValue(nums []int) int {
    s, cnt := 0, 0
    for _, n := range nums {
        if n%2 == 0 && n%3 == 0 {
            s += n
            cnt++
        }
    }
    if cnt == 0 {
        return 0
    }
    return s / cnt
}
Java
class Solution {
    public int averageValue(int[] nums) {
        int s = 0, cnt = 0;
        for (int n : nums) {
            if (n % 2 == 0 && n % 3 == 0) {
                s += n;
                cnt++;
            }
        }
        return cnt == 0 ? 0 : s / cnt;
    }
}
Kotlin
class Solution {
   fun averageValue(nums: IntArray): Int {
      var s = 0
      var cnt = 0
      for (n in nums) {
        if (n % 2 == 0 && n % 3 == 0) {
           s += n
           cnt++
        }
      }
      return if (cnt == 0) 0 else s / cnt
   }
}
Python
class Solution:
   def averageValue(self, nums: list[int]) -> int:
      s: int = 0
      cnt: int = 0
      for n in nums:
        if n % 2 == 0 and n % 3 == 0:
           s += n
           cnt += 1
      return 0 if cnt == 0 else s // cnt
Rust
impl Solution {
   pub fn average_value(nums: Vec<i32>) -> i32 {
      let (mut s, mut cnt) = (0, 0);
      for n in nums {
        if n % 2 == 0 && n % 3 == 0 {
           s += n;
           cnt += 1;
        }
      }
      if cnt == 0 { 0 } else { s / cnt }
   }
}

Complexity

  • ⏰ Time complexity: O(n) — Each element is checked once.
  • 🧺 Space complexity: O(1) — Only a few variables are used.

Comments