Advertisement

Average Value of Even Numbers That Are Divisible by Three - LeetCode 2455 Solution

Average Value of Even Numbers That Are Divisible by Three - Complete Solution Guide

Average Value of Even Numbers That Are Divisible by Three is LeetCode problem 2455, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 ret

Detailed Explanation

The problem asks you to calculate the average of all even numbers in an input array that are also divisible by 3. The input is an array of positive integers (`nums`). The output is a single integer representing the average, rounded down to the nearest integer. If no such numbers exist, the output is 0. The constraints limit the array size and the range of numbers within the array.

Solution Approach

The provided solutions utilize a straightforward iterative approach. They iterate through the input array, checking each number for the specified conditions (even and divisible by 3). If a number meets both conditions, it's added to a running sum, and a counter is incremented. After iterating through the entire array, the average is calculated by dividing the sum by the count. A check for a zero count prevents division by zero errors.

Step-by-Step Algorithm

  1. Step 1: Initialize two variables: `sum` (to accumulate the sum of eligible numbers) and `count` (to count the number of eligible numbers). Both are initialized to 0.
  2. Step 2: Iterate through the input array `nums`.
  3. Step 3: For each number `num`, check if it's even (`num % 2 == 0`) and divisible by 3 (`num % 3 == 0`).
  4. Step 4: If both conditions are true, add `num` to `sum` and increment `count`.
  5. Step 5: After iterating through all numbers, check if `count` is 0. If it is, return 0 (no eligible numbers).
  6. Step 6: Otherwise, calculate the average by performing integer division (`sum / count`) and return the result.

Key Insights

  • Insight 1: The problem requires checking two conditions for each number: whether it's even (divisible by 2) and whether it's divisible by 3. This can be efficiently done using the modulo operator (%) in a single `if` condition.
  • Insight 2: A simple iterative approach is sufficient. No complex data structures are needed.
  • Insight 3: Handling the edge case where no numbers satisfy the conditions is crucial. The code should explicitly check if the count of eligible numbers is zero before calculating the average to prevent division by zero errors.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Math.

Companies

Asked at: Accenture, IBM.