You are given a 0-indexed integer array nums
, where nums[i]
represents the score of the ith
student. You are also given an integer k
.
Pick the scores of any k
students from the array so that the difference between the highest and the lowest of the k
scores is minimized.
Return the minimum possible difference.
Example 1:
Input: nums = [90], k = 1 Output: 0 Explanation: There is one way to pick score(s) of one student: - [90]. The difference between the highest and lowest score is 90 - 90 = 0. The minimum possible difference is 0.
Example 2:
Input: nums = [9,4,1,7], k = 2 Output: 2 Explanation: There are six ways to pick score(s) of two students: - [9,4,1,7]. The difference between the highest and lowest score is 9 - 4 = 5. - [9,4,1,7]. The difference between the highest and lowest score is 9 - 1 = 8. - [9,4,1,7]. The difference between the highest and lowest score is 9 - 7 = 2. - [9,4,1,7]. The difference between the highest and lowest score is 4 - 1 = 3. - [9,4,1,7]. The difference between the highest and lowest score is 7 - 4 = 3. - [9,4,1,7]. The difference between the highest and lowest score is 7 - 1 = 6. The minimum possible difference is 2.
Constraints:
1 <= k <= nums.length <= 1000
0 <= nums[i] <= 105
program main
implicit none
integer, parameter :: n = 10
integer, dimension(n) :: nums
integer :: k
integer :: i
! Examples
nums = reshape([90, 9, 4, 1, 7, 6, 5, 4, 6, 7], shape(nums))
k = 1
write(*,*) 'Example 1:'
call solve(nums, k)
write(*,*) ''
nums = reshape([9, 4, 1, 7], shape(nums))
k = 2
write(*,*) 'Example 2:'
call solve(nums, k)
write(*,*) ''
contains
subroutine solve(nums, k)
implicit none
integer, dimension(:), intent(in) :: nums
integer, intent(in) :: k
integer :: min_diff
if (size(nums) < k .or. k < 1) then
write(*,*) 'Invalid input'
return
end if
min_diff = huge(0)
do i = 1, size(nums) - k + 1
min_diff = min(min_diff, maxval(nums(i:i+k-1)) - minval(nums(i:i+k-1)))
end do
write(*,*) 'Minimum possible difference: ', min_diff
end subroutine solve
end program main
temp.f95:15:19: 15 | nums = reshape([9, 4, 1, 7], shape(nums)) | 1 Error: Without padding, there are not enough elements in the intrinsic RESHAPE source at (1) to match the shape
module MinimumDifference
implicit none
private
public :: minimumDifference
contains
function minimumDifference(nums, k) result(minDiff)
integer, intent(in) :: nums(:)
integer, intent(in) :: k
integer :: minDiff
integer :: i, j, n, temp
! Sort the array in ascending order
do i = 1, size(nums) - 1
do j = i + 1, size(nums)
if (nums(i) > nums(j)) then
temp = nums(i)
nums(i) = nums(j)
nums(j) = temp
end if
end do
end do
! Initialize the minimum difference
minDiff = huge(0)
! Loop through all possible combinations of k elements
do i = 1, size(nums) - k + 1
n = nums(i + k - 1) - nums(i)
if (n < minDiff) then
minDiff = n
end if
end do
end function minimumDifference
end module MinimumDifference
! Test the module with the given examples
program testMinimumDifference
use MinimumDifference
implicit none
integer, parameter :: nums1(1) = [90]
integer, parameter :: nums2(4) = [9, 4, 1, 7]
integer, parameter :: nums3(5) = [3, 4, 5, 1, 2]
integer, parameter :: nums4(6) = [9, 4, 1, 7, 9, 4]
integer, parameter :: k1 = 1
integer, parameter :: k2 = 2
integer, parameter :: k3 = 3
integer, parameter :: k4 = 4
integer :: minDiff
! Test case 1:
minDiff = minimumDifference(nums1, k1)
if (minDiff /= 0) then
write (*,*) "Test case 1 failed. Expected minimum difference = 0. Got", minDiff
stop 1
end if
! Test case 2:
minDiff = minimumDifference(nums2, k2)
if (minDiff /= 2) then
write (*,*) "Test case 2 failed. Expected minimum difference = 2. Got", minDiff
stop 1
end if
! Test case 3:
minDiff = minimumDifference(nums3, k3)
if (minDiff /= 2) then
write (*,*) "Test case 3 failed. Expected minimum difference = 2. Got", minDiff
stop 1
end if
! Test case 4:
minDiff = minimumDifference(nums4, k4)
if (minDiff /= 2) then
write (*,*) "Test case 4 failed. Expected minimum difference = 2. Got", minDiff
stop 1
end if
write (*,*) "All test cases passed!"
end program testMinimumDifference
temp.f95:4:31: 4 | public :: minimumDifference | 1 Error: PUBLIC attribute applied to MODULE minimumdifference at (1) temp.f95:6:30: 6 | function minimumDifference(nums, k) result(minDiff) | 1 Error: MODULE attribute of ‘minimumdifference’ 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:32: 8 | integer, intent(in) :: k | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:9:26: 9 | integer :: minDiff | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:32: 10 | integer :: i, j, n, temp | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:13:32: 13 | do i = 1, size(nums) - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:14:36: 14 | do j = i + 1, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:15:43: 15 | if (nums(i) > nums(j)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:16:34: 16 | temp = nums(i) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:17:37: 17 | nums(i) = nums(j) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:18:34: 18 | nums(j) = temp | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:19:19: 19 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:20:15: 20 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:21:11: 21 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:24:25: 24 | minDiff = huge(0) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:27:36: 27 | do i = 1, size(nums) - k + 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:28:41: 28 | n = nums(i + k - 1) - nums(i) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:29:33: 29 | if (n < minDiff) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:30:27: 30 | minDiff = n | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:31:15: 31 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:32:11: 32 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:33:7: 33 | end function minimumDifference | 1 Error: Expecting END MODULE statement at (1) temp.f95:38:9: 38 | use MinimumDifference | 1 Fatal Error: Cannot open module file ‘minimumdifference.mod’ for reading at (1): No such file or directory compilation terminated.
def max_distance(nums1, nums2):
i, j, max_dist = 0, 0, 0
while i < len(nums1) and j < len(nums2):
if i <= j and nums1[i] <= nums2[j]:
max_dist = max(max_dist, j - i)
j += 1
else:
i += 1
return max_dist
i
and j
to 0 and maxDist
to 0.while
condition until we reach the end of either of the arrays.i <= j
and nums1[i] <= nums2[j]
.j - i
and update maxDist
if this distance is greater than its current value.j
to check the next element in array nums2
.i
to check the next element in array nums1
.maxDist
as the maximum distance of a valid pair.int maxDistance(vector<int>& nums1, vector<int>& nums2) {
int i = 0, j = 0, maxDist = 0;
while (i < nums1.size() && j < nums2.size()) {
if (i <= j && nums1[i] <= nums2[j]) {
maxDist = max(maxDist, j - i);
j++;
} else {
i++;
}
}
return maxDist;
}
i
and j
to 0 and maxDist
to 0.while
condition until we reach the end of either of the arrays.i <= j
and nums1[i] <= nums2[j]
.j - i
and update maxDist
if this distance is greater than its current value.j
to check the next element in array nums2
.i
to check the next element in array nums1
.maxDist
as the maximum distance of a valid pair.