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.
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.
Input: nums = [1,2,4,7,10] Output: 0 Explanation: There is no single number that satisfies the requirement, so return 0.
1 <= nums.length <= 1000
1 <= nums[i] <= 1000
impl Solution {
pub fn average_value(nums: Vec<i32>) -> i32 {
let sum = nums.iter().filter(|&&x| x % 6 == 0).sum::<i32>();
let len = nums.iter().filter(|&&x| x % 6 == 0).count() as i32;
sum.checked_div(len).unwrap_or(0)
}
}