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 <= 10001 <= nums[i] <= 1000When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to find the average of some special numbers from a collection. The brute force method means we'll look at each number individually to see if it's special, and then do some math.
Here's how the algorithm would work step-by-step:
def average_of_even_divisible_by_three(numbers):
sum_of_valid_numbers = 0
count_of_valid_numbers = 0
for number in numbers:
# Check if the number is even.
if number % 2 == 0:
# Check if the number is divisible by three.
if number % 3 == 0:
sum_of_valid_numbers += number
count_of_valid_numbers += 1
# Avoid division by zero if no numbers meet the criteria.
if count_of_valid_numbers == 0:
return 0
average = sum_of_valid_numbers / count_of_valid_numbers
return averageTo efficiently find the average, we only consider numbers that meet both conditions: being even and divisible by three. We track only the necessary information which drastically reduces processing.
Here's how the algorithm would work step-by-step:
def average_value_of_even_numbers_divisible_by_three(numbers):
sum_of_valid_numbers = 0
count_of_valid_numbers = 0
for number in numbers:
# Only consider even numbers.
if number % 2 == 0:
# Further filter for divisibility by 3.
if number % 3 == 0:
# Accumulate the number and increment the count.
sum_of_valid_numbers += number
count_of_valid_numbers += 1
# Avoid division by zero if no valid numbers are found.
if count_of_valid_numbers == 0:
return 0
average = sum_of_valid_numbers / count_of_valid_numbers
return average| Case | How to Handle |
|---|---|
| Empty input array | Return 0 immediately as there are no numbers to process. |
| Array contains no numbers divisible by both 2 and 3 | Return 0 as per the problem statement when no qualifying numbers are found. |
| Array contains only one element that is divisible by both 2 and 3 | Calculate the average (which is just the number itself) and return it as an integer. |
| Array contains extremely large positive integers | Ensure that the sum of the qualifying numbers does not cause integer overflow; use a larger data type if necessary. |
| Array contains a mix of positive integers including some that are both even and divisible by three. | The standard solution logic correctly filters and averages these numbers. |
| All numbers in the array are even and divisible by three. | Calculate the average of all the numbers. |
| The average is not a whole number | Truncate the floating-point average to an integer, according to the problem description. |
| Null input array | Throw an IllegalArgumentException (or equivalent for the language being used) or return 0 after logging an error, depending on requirements. |