Skip to main content

Average Value of Even Numbers That Are Divisible by Three - Solution & Explanation

EasyArrayMath13 min readAsked at: IBM, Google
Practice this problem

Problem Statement

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

Approach Overview

Problem Overview: Given an integer array, compute the average of values that are both even and divisible by three. If no such numbers exist, return 0. The task reduces to filtering numbers divisible by 6, tracking their sum and count, then returning sum / count.

Approach 1: Recursive Approach with Memoization (O(n) time, O(n) space)

This approach processes the array using recursion. At each index, check whether the current value is divisible by 6. If it is, update the running sum and count. The recursive function moves to the next index until the end of the array. Memoization stores intermediate states such as (index) to avoid recomputation if the recursion revisits the same position.

The key idea is breaking the problem into smaller subproblems: compute the valid sum and count from the remaining suffix of the array. After recursion finishes, calculate the average using the aggregated values. Time complexity is O(n) since each element is processed once, while recursion and memo storage require O(n) space. This method demonstrates recursive problem decomposition on array traversal.

Approach 2: Iterative Dynamic Programming Approach (O(n) time, O(1) space)

The optimal solution uses a single pass through the array. Maintain two variables: sum for the total of qualifying numbers and count for how many satisfy the condition. During iteration, check num % 6 == 0. This works because a number divisible by both 2 and 3 must be divisible by 6.

Each valid number updates sum += num and count++. After finishing the loop, return sum / count if count > 0; otherwise return 0. This approach leverages basic properties of math and linear scanning of an array. Since only two variables are maintained, space complexity remains constant at O(1) while time complexity is O(n).

Recommended for interviews: The iterative single-pass solution is what interviewers expect. It demonstrates that you recognize the mathematical shortcut (divisible by 6) and can compute aggregates in one traversal. Mentioning the recursive variant shows understanding of alternative traversal strategies, but the constant-space iterative approach is the most practical and efficient.

Approach 1: Recursive Approach with Memoization

This approach uses recursion to solve the problem by breaking it down into smaller subproblems. To optimize, we use memoization to store the results of already solved sub-problems, thus avoiding redundant calculations.

In this C program, we define a `fibonacci` function that calculates the n-th Fibonacci number using recursion and memoization. The global `memo` array is initialized to `-1` to denote unsolved subproblems. This approach ensures that each subproblem is solved only once.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n)
Space Complexity: O(n) due to the memo array.

Try this approach in the editor →

Approach 2: Iterative Dynamic Programming Approach

This approach utilizes dynamic programming to solve the problem iteratively. We construct a table to store results of subproblems and use these results to solve the problem iteratively while minimizing space usage.

This C implementation uses an array to store Fibonacci numbers up to n. By iteratively filling the array based on the relation F(i) = F(i-1) + F(i-2), it efficiently computes the desired number.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n)
Space Complexity: O(n)

Try this approach in the editor →

Approach 3: 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).

Code

Python

Java

C++

Go

TypeScript

Rust

C

Try this approach in the editor →

Approach 4: Default Approach

Code

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Recursive Approach with Memoization

Time Complexity: O(n)
Space Complexity: O(n) due to the memo array.

Iterative Dynamic Programming Approach

Time Complexity: O(n)
Space Complexity: O(n)

Simulation—
Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Recursive Approach with MemoizationO(n)O(n)Useful for practicing recursive array traversal or when extending the problem to more complex state tracking
Iterative Dynamic Programming (Single Pass)O(n)O(1)Best general solution; minimal memory and fastest implementation for scanning arrays

Video Solution

2455. Average Value of Even Numbers That Are Divisible by Three | Weekly Contest 317 | LeetCode 2455 • Bro Coders • 531 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Average Value of Even Numbers That Are Divisible by Three easy or hard?
Average Value of Even Numbers That Are Divisible by Three is classified as an Easy problem. It focuses on basic array traversal, simple divisibility checks, and maintaining running totals, making it suitable for beginners practicing array and math fundamentals.
Average Value of Even Numbers That Are Divisible by Three Python/Java solution
In Python or Java, iterate through the array and check num % 6 == 0. Maintain sum and count variables, update them for each valid element, and return sum // count (or sum / count depending on language rules) if count is non-zero; otherwise return 0.
How to solve Average Value of Even Numbers That Are Divisible by Three in O(n)?
Iterate through the array once and check if each element satisfies num % 6 == 0. If it does, add it to a running sum and increment a counter. After the loop, return sum / count if count is greater than zero; otherwise return 0.
What is the best approach for Average Value of Even Numbers That Are Divisible by Three?
The best approach is a single-pass iteration over the array. Track the sum and count of numbers divisible by 6, since divisibility by both 2 and 3 implies divisibility by 6. This solution runs in O(n) time and O(1) space and is typically expected in interviews.
Is Average Value of Even Numbers That Are Divisible by Three asked at Google/Amazon/Meta?
This exact problem may appear on practice platforms like LeetCode as an easy-level exercise. Variations involving filtering arrays and computing aggregates are common in coding screens at companies like Amazon and Google because they test basic array traversal and condition checks.
What data structure is used in Average Value of Even Numbers That Are Divisible by Three?
The problem primarily uses an array as the input data structure. The algorithm performs a linear scan over the array and maintains simple scalar variables for sum and count, without requiring additional data structures.
What is the time complexity of Average Value of Even Numbers That Are Divisible by Three?
The optimal solution runs in O(n) time where n is the length of the array. Each element is checked once for the condition num % 6 == 0. Space complexity is O(1) because only two variables (sum and count) are maintained.

Ready to solve this problem?

Practice Average Value of Even Numbers That Are Divisible by Three with our built-in code editor and test cases.

Practice on FleetCode