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
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
At line 10 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x78f6d0850960 in ??? #1 0x78f6d08514d9 in ??? #2 0x78f6d0aa517b in ??? #3 0x78f6d0a9e684 in ??? #4 0x78f6d0a9f2aa in ??? #5 0x5574cc6f7272 in MAIN__ #6 0x5574cc6f75e4 in main
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
temp.f95:7:39: 7 | function average_of_even_divisible_by_3(nums) result(average) | 1 Error: MODULE attribute of ‘average_of_even_divisible_by_3’ conflicts with PROCEDURE attribute at (1) temp.f95:9:30: 9 | integer, intent(in) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:15: 10 | real :: average | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:21: 12 | integer :: i, j, k, n | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:14:14: 14 | n = size(nums) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:16:11: 16 | average = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:18:11: 18 | do i = 1, n | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:19:61: 19 | if (mod(nums(i), 2) == 0 .and. mod(nums(i), 3) == 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:20:35: 20 | average = average + nums(i) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:21:7: 21 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:22:3: 22 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:24:27: 24 | average = average / real(n) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:26:3: 26 | end function average_of_even_divisible_by_3 | 1 Error: Expecting END MODULE statement at (1) temp.f95:32:5: 32 | use average_of_even_divisible_by_3 | 1 Fatal Error: Cannot open module file ‘average_of_even_divisible_by_3.mod’ for reading at (1): No such file or directory compilation terminated.
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.
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.