You are given a 0-indexed integer array nums
of even length.
As long as nums
is not empty, you must repetitively:
nums
and remove it.nums
and remove it.The average of two numbers a
and b
is (a + b) / 2
.
2
and 3
is (2 + 3) / 2 = 2.5
.Return the number of distinct averages calculated using the above process.
Note that when there is a tie for a minimum or maximum number, any can be removed.
Example 1:
Input: nums = [4,1,4,0,3,5] Output: 2 Explanation: 1. Remove 0 and 5, and the average is (0 + 5) / 2 = 2.5. Now, nums = [4,1,4,3]. 2. Remove 1 and 4. The average is (1 + 4) / 2 = 2.5, and nums = [4,3]. 3. Remove 3 and 4, and the average is (3 + 4) / 2 = 3.5. Since there are 2 distinct numbers among 2.5, 2.5, and 3.5, we return 2.
Example 2:
Input: nums = [1,100] Output: 1 Explanation: There is only one average to be calculated after removing 1 and 100, so we return 1.
Constraints:
2 <= nums.length <= 100
nums.length
is even.0 <= nums[i] <= 100
program main
implicit none
integer :: i, j, k, n, m, min_idx, max_idx, avg
integer, dimension(100) :: nums
logical, dimension(100) :: used
! read input
read *, n
do i = 1, n
read *, nums(i)
end do
! solve problem
m = 0
do while (size(nums) > 0)
! find minimum
min_idx = -1
do i = 1, size(nums)
if (min_idx == -1 .or. nums(i) < nums(min_idx)) then
min_idx = i
end if
end do
! find maximum
max_idx = -1
do i = 1, size(nums)
if (max_idx == -1 .or. nums(i) > nums(max_idx)) then
max_idx = i
end if
end do
! calculate average
avg = (nums(min_idx) + nums(max_idx)) / 2
! remove minimum and maximum
used(min_idx) = .true.
used(max_idx) = .true.
nums(min_idx) = nums(size(nums))
nums(max_idx) = nums(size(nums))
nums(:) = nums(:) - 1
! count unique averages
if (.not. any(used)) then
m = m + 1
else
do i = 1, size(used)
if (.not. used(i)) then
exit
end if
end do
m = m + 1
end if
end do
! print output
write (*, '(I0)') m
end program main
At line 9 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7e3dbab7f960 in ??? #1 0x7e3dbab804d9 in ??? #2 0x7e3dbadd417b in ??? #3 0x7e3dbadcd684 in ??? #4 0x7e3dbadce2aa in ??? #5 0x564d660171da in MAIN__ #6 0x564d660174b6 in main
module avg_of_min_max
implicit none
private
public :: avg_of_min_max
contains
function avg_of_min_max(nums) result(ans)
integer, intent(in) :: nums(:)
integer :: ans
integer :: min_idx, max_idx, avg
ans = 0
do while (size(nums) > 0)
min_idx = minloc(nums, 1)
max_idx = maxloc(nums, 1)
avg = (nums(min_idx) + nums(max_idx)) / 2
ans = ans + 1
nums(min_idx) = -1
nums(max_idx) = -1
end do
end function avg_of_min_max
end module avg_of_min_max
program test_avg_of_min_max
use avg_of_min_max
implicit none
integer, parameter :: nums1(4) = [4, 1, 4, 0, 3, 5]
integer, parameter :: nums2(2) = [1, 100]
integer :: ans1, ans2
ans1 = avg_of_min_max(nums1)
ans2 = avg_of_min_max(nums2)
write (*,*) "Test 1:", ans1
write (*,*) "Test 2:", ans2
end program test_avg_of_min_max
temp.f95:4:28: 4 | public :: avg_of_min_max | 1 Error: PUBLIC attribute applied to MODULE avg_of_min_max at (1) temp.f95:6:27: 6 | function avg_of_min_max(nums) result(ans) | 1 Error: MODULE attribute of ‘avg_of_min_max’ conflicts with PROCEDURE attribute at (1) temp.f95:7:38: 7 | integer, intent(in) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:8:22: 8 | integer :: ans | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:9:40: 9 | integer :: min_idx, max_idx, avg | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:11:15: 11 | ans = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:12:33: 12 | do while (size(nums) > 0) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:13:37: 13 | min_idx = minloc(nums, 1) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:14:37: 14 | max_idx = maxloc(nums, 1) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:15:53: 15 | avg = (nums(min_idx) + nums(max_idx)) / 2 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:16:25: 16 | ans = ans + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:17:30: 17 | nums(min_idx) = -1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:18:30: 18 | nums(max_idx) = -1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:19:11: 19 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:20:7: 20 | end function avg_of_min_max | 1 Error: Expecting END MODULE statement at (1) temp.f95:24:9: 24 | use avg_of_min_max | 1 Fatal Error: Cannot open module file ‘avg_of_min_max.mod’ for reading at (1): No such file or directory compilation terminated.
def distinctAverages(nums):
averages = set()
nums.sort()
n = len(nums)
for i in range(n // 2):
averages.add((nums[i] + nums[n - 1 - i]) / 2.0)
return len(averages)
int distinctAverages(vector<int>& nums) {
set<double> averages;
sort(nums.begin(), nums.end());
int n = nums.size();
for (int i = 0; i < n / 2; i++) {
averages.insert((nums[i] + nums[n - 1 - i]) / 2.0);
}
return averages.size();
}