Average Value of Even Numbers That Are Divisible by Three

🏠 ⬅️ ➡️

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

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: dp = selected_real_kind(15, 307)

    integer, dimension(:), allocatable :: nums
    integer :: n, i, j, k, sum, avg

    ! read input
    read (*, *) n
    allocate (nums(n))
    do i = 1, n
        read (*, *) nums(i)
    end do

    ! solve problem
    sum = 0
    do i = 1, n
        if (mod(nums(i), 2) == 0 .and. mod(nums(i), 3) == 0) then
            sum = sum + nums(i)
        end if
    end do
    avg = sum / n

    ! print output
    write (*, '(I0)') avg

end program main
Compiled
Executed
Correct
module average_of_even_divisible_by_3

implicit none

contains

function average_of_even_divisible_by_3(nums) result(average)

integer, intent(in) :: nums(:)
real :: average

integer :: i, j, k, n

n = size(nums)

average = 0

do i = 1, n
    if (mod(nums(i), 2) == 0 .and. mod(nums(i), 3) == 0) then
        average = average + nums(i)
    end if
end do

average = average / real(n)

end function average_of_even_divisible_by_3

end module average_of_even_divisible_by_3

program test

use average_of_even_divisible_by_3

implicit none

integer, parameter :: nums1(5) = [1, 3, 6, 10, 12, 15]
integer, parameter :: nums2(3) = [1, 2, 4]
integer, parameter :: nums3(1) = [1]

write (*, '(a, f0.2)') 'Average of even divisible by 3: ', &
    average_of_even_divisible_by_3(nums1)
write (*, '(a, f0.2)') 'Average of even divisible by 3: ', &
    average_of_even_divisible_by_3(nums2)
write (*, '(a, f0.2)') 'Average of even divisible by 3: ', &
    average_of_even_divisible_by_3(nums3)

end program test
🌐 Data from online sources
def average_even_divisible_by_three(nums):
    total = 0
    count = 0
    for num in nums:
        if num % 2 == 0 and num % 3 == 0:
            total += num
            count += 1
    return total // count if count > 0 else 0

The algorithm iterates through the input array, checking if an element is even (num % 2 == 0) and divisible by 3 (num % 3 == 0). If the element satisfies both conditions, it is added to the sum and the count is incremented. After the iteration, if count is greater than 0, the average is calculated by dividing the sum by count. The result is returned as an integer by rounding down the division result in all languages. If count is 0, meaning there were no elements that satisfied the conditions, the function returns 0.

🌐 Data from online sources
int averageEvenDivisibleByThree(const std::vector<int>& nums) {
    int sum = 0;
    int count = 0;
    for (int num : nums) {
        if (num % 2 == 0 && num % 3 == 0) {
            sum += num;
            count++;
        }
    }
    return count == 0 ? 0 : sum / count;
}

The algorithm iterates through the input array, checking if an element is even (num % 2 == 0) and divisible by 3 (num % 3 == 0). If the element satisfies both conditions, it is added to the sum and the count is incremented. After the iteration, if count is greater than 0, the average is calculated by dividing the sum by count. The result is returned as an integer by rounding down the division result in all languages. If count is 0, meaning there were no elements that satisfied the conditions, the function returns 0.