LeetCode Problem Workspace

Average Value of Even Numbers That Are Divisible by Three

Find the average of even numbers divisible by 3 from an array of positive integers.

category

2

Topics

code_blocks

7

Code langs

hub

3

Related

Practice Focus

Easy · Array plus Math

bolt

Answer-first summary

Find the average of even numbers divisible by 3 from an array of positive integers.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Array plus Math

Try AiBox Copilotarrow_forward

The task requires calculating the average of even numbers divisible by 3 from a given array. First, filter out the even numbers divisible by 3, then calculate their average and return the result, rounded down to the nearest integer.

Problem Statement

You are given an array of positive integers. Your task is to find the average value of all even numbers that are divisible by 3. If no such numbers exist, return 0. The average should be the sum of these numbers divided by their count, rounded down to the nearest integer.

For example, if the input array is [1,3,6,10,12,15], the even numbers divisible by 3 are 6 and 12. The average of these numbers is (6 + 12) / 2 = 9. If there are no numbers that meet the criteria, return 0.

Examples

Example 1

Input: nums = [1,3,6,10,12,15]

Output: 9

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

There is no single number that satisfies the requirement, so return 0.

Constraints

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

Solution Approach

Filter and Average Calculation

Filter the given array to retain only even numbers divisible by 3. Then, calculate the sum of these numbers and divide by their count. Round the result down to the nearest integer.

Edge Case Handling

Consider the edge case where no even numbers divisible by 3 exist in the array. In such cases, return 0 as the average value.

Optimized Iteration

Iterate through the array once and check each number for the properties of being even and divisible by 3. This ensures a time-efficient solution with a linear complexity.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

The time complexity is O(n) due to a single pass through the array to filter the numbers, where n is the length of the array. The space complexity is O(1) as only a few variables are used to store the sum and count of valid numbers.

What Interviewers Usually Probe

  • Understand how to filter specific conditions in an array
  • Ability to perform simple mathematical calculations efficiently
  • Handle edge cases like empty or invalid results

Common Pitfalls or Variants

Common pitfalls

  • Misunderstanding the filtering condition, forgetting that a number must be both even and divisible by 3
  • Not correctly handling cases where no valid numbers exist
  • Incorrectly rounding the result, when it should be rounded down to the nearest integer

Follow-up variants

  • Instead of rounding down, round to the nearest integer
  • Use bitwise operations for checking even numbers
  • Optimize space complexity by avoiding extra variables

FAQ

What is the pattern for solving 'Average Value of Even Numbers That Are Divisible by Three'?

The problem uses a combination of array manipulation and basic mathematical operations. It requires filtering the array for even numbers divisible by 3, then calculating and rounding the average.

How do I handle edge cases in this problem?

Edge cases include scenarios where no numbers in the array meet the criteria (even and divisible by 3). In such cases, you should return 0.

What is the time complexity of solving this problem?

The time complexity is O(n) because we iterate through the array once to filter and calculate the sum of the valid numbers.

What happens if there are no valid numbers?

If there are no even numbers divisible by 3, return 0 as the result.

How does GhostInterview help in solving this problem?

GhostInterview helps by guiding you through efficient filtering, edge case handling, and optimizing your solution in terms of time and space complexity.

terminal

Solution

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.

1
2
3
4
5
6
7
8
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

Solution 2

#### Rust

1
2
3
4
5
6
7
8
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
Average Value of Even Numbers That Are Divisible by Three Solution: Array plus Math | LeetCode #2455 Easy