#2455
Easy Algorithms Average value of even numbers that are divisible by three
Array Math
62.9% acceptance
Feb 25, 2026
374
42
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. *
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn average_value(nums: Vec<i32>) -> i32 {
// divisible by 6 (even AND divisible by 3)
let filtered: Vec<i32> = nums.into_iter().filter(|&x| x % 6 == 0).collect();
if filtered.is_empty() { return 0; }
filtered.iter().sum::<i32>() / filtered.len() as i32
}
}