返回介绍

solution / 2400-2499 / 2455.Average Value of Even Numbers That Are Divisible by Three / README_EN

发布于 2024-06-17 01:03:05 字数 4231 浏览 0 评论 0 收藏 0

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

中文文档

Description

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

Solutions

Solution 1: Simulation

We notice that an even number divisible by $3$ must be a multiple of $6$. Therefore, we only need to traverse the array, count the sum and the number of all multiples of $6$, and then calculate the average.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

class Solution:
  def averageValue(self, nums: List[int]) -> int:
    s = n = 0
    for x in nums:
      if x % 6 == 0:
        s += x
        n += 1
    return 0 if n == 0 else s // n
class Solution {
  public int averageValue(int[] nums) {
    int s = 0, n = 0;
    for (int x : nums) {
      if (x % 6 == 0) {
        s += x;
        ++n;
      }
    }
    return n == 0 ? 0 : s / n;
  }
}
class Solution {
public:
  int averageValue(vector<int>& nums) {
    int s = 0, n = 0;
    for (int x : nums) {
      if (x % 6 == 0) {
        s += x;
        ++n;
      }
    }
    return n == 0 ? 0 : s / n;
  }
};
func averageValue(nums []int) int {
  var s, n int
  for _, x := range nums {
    if x%6 == 0 {
      s += x
      n++
    }
  }
  if n == 0 {
    return 0
  }
  return s / n
}
function averageValue(nums: number[]): number {
  let s = 0;
  let n = 0;
  for (const x of nums) {
    if (x % 6 === 0) {
      s += x;
      ++n;
    }
  }
  return n === 0 ? 0 : ~~(s / n);
}
impl Solution {
  pub fn average_value(nums: Vec<i32>) -> i32 {
    let mut s = 0;
    let mut n = 0;
    for x in nums.iter() {
      if x % 6 == 0 {
        s += x;
        n += 1;
      }
    }
    if n == 0 {
      return 0;
    }
    s / n
  }
}
int averageValue(int* nums, int numsSize) {
  int s = 0, n = 0;
  for (int i = 0; i < numsSize; ++i) {
    if (nums[i] % 6 == 0) {
      s += nums[i];
      ++n;
    }
  }
  return n == 0 ? 0 : s / n;
}

Solution 2

impl Solution {
  pub fn average_value(nums: Vec<i32>) -> i32 {
    let filtered_nums: Vec<i32> = nums
      .iter()
      .cloned()
      .filter(|&n| n % 6 == 0)
      .collect();

    if filtered_nums.is_empty() {
      return 0;
    }

    filtered_nums.iter().sum::<i32>() / (filtered_nums.len() as i32)
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文