##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2455.html

# 2455. Average Value of Even Numbers That Are Divisible by Three

• Difficulty: Easy.
• Related Topics: Array, Math.
• Similar Questions: Binary Prefix Divisible By 5.

## 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.

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 (Java, C++, Python)

• class Solution {
public int averageValue(int[] nums) {
int s = 0, n = 0;
for (int v : nums) {
if (v % 6 == 0) {
s += v;
++n;
}
}
return n == 0 ? 0 : s / n;
}
}

• class Solution {
public:
int averageValue(vector<int>& nums) {
int s = 0, n = 0;
for (int v : nums) {
if (v % 6 == 0) {
s += v;
++n;
}
}
return n == 0 ? 0 : s / n;
}
};

• class Solution:
def averageValue(self, nums: List[int]) -> int:
s = n = 0
for v in nums:
if v % 6 == 0:
s += v
n += 1
return 0 if n == 0 else s // n


• func averageValue(nums []int) int {
s, n := 0, 0
for _, v := range nums {
if v%6 == 0 {
s += v
n++
}
}
if n == 0 {
return 0
}
return s / n
}

• function averageValue(nums: number[]): number {
let sum = 0;
let n = 0;
for (const num of nums) {
if (num % 6 === 0) {
sum += num;
n++;
}
}

if (n === 0) {
return 0;
}
return Math.floor(sum / n);
}


• impl Solution {
pub fn average_value(nums: Vec<i32>) -> i32 {
let mut sum = 0;
let mut n = 0;
for num in nums.iter() {
if num % 6 == 0 {
sum += num;
n += 1;
}
}

if n == 0 {
return 0;
}
sum / n
}
}



Explain:

nope.

Complexity:

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