Given an integer array nums
and an integer k
, return true
if there are two distinct indices i
and j
in the array such that nums[i] == nums[j]
and abs(i - j) <= k
.
Example 1:
Input: nums = [1,2,3,1], k = 3 Output: true
Example 2:
Input: nums = [1,0,1,1], k = 1 Output: true
Example 3:
Input: nums = [1,2,3,1,2,3], k = 2 Output: false
Constraints:
1 <= nums.length <= 105
-109 <= nums[i] <= 109
0 <= k <= 105
program main
implicit none
integer, parameter :: n = 4
integer, dimension(n) :: nums
integer :: k
logical :: result
! Example 1
nums = [1, 2, 3, 1]
k = 3
result = contains_duplicate(nums, k)
print '(L1)', result
! Example 2
nums = [1, 0, 1, 1]
k = 1
result = contains_duplicate(nums, k)
print '(L1)', result
! Example 3
nums = [1, 2, 3, 1, 2, 3]
k = 2
result = contains_duplicate(nums, k)
print '(L1)', result
contains
function contains_duplicate(nums, k) result(res)
implicit none
integer, dimension(:), intent(in) :: nums
integer, intent(in) :: k
logical :: res
integer :: i, j
res = .false.
do i = 1, size(nums) - 1
do j = i + 1, size(nums)
if (nums(i) == nums(j)) then
if (abs(i - j) <= k) then
res = .true.
exit
end if
end if
end do
end do
end function contains_duplicate
end program main
temp.f95:22:4: 22 | nums = [1, 2, 3, 1, 2, 3] | 1 Error: Different shape for array assignment at (1) on dimension 1 (4 and 6)
module has_duplicate_value_within_k
implicit none
private
public :: has_duplicate_value_within_k
contains
logical function has_duplicate_value_within_k(nums, k) result(has_duplicate)
integer, intent(in) :: nums(:)
integer, intent(in) :: k
integer :: i, j
has_duplicate = .false.
do i = 1, size(nums) - 1
do j = i + 1, size(nums)
if (nums(i) == nums(j) .and. abs(i - j) <= k) then
has_duplicate = .true.
exit
end if
end do
if (has_duplicate) exit
end do
end function has_duplicate_value_within_k
end module has_duplicate_value_within_k
program test_has_duplicate_value_within_k
use has_duplicate_value_within_k
implicit none
integer, parameter :: nums1(4) = [1, 2, 3, 1]
integer, parameter :: nums2(5) = [1, 0, 1, 1, 2]
integer, parameter :: nums3(6) = [1, 2, 3, 1, 2, 3]
integer, parameter :: k1 = 3
integer, parameter :: k2 = 1
integer, parameter :: k3 = 2
write (*, '(A)') 'Test 1:'
if (has_duplicate_value_within_k(nums1, k1)) then
write (*, '(A)') 'Passed!'
else
write (*, '(A)') 'Failed!'
end if
write (*, '(A)') 'Test 2:'
if (has_duplicate_value_within_k(nums2, k2)) then
write (*, '(A)') 'Passed!'
else
write (*, '(A)') 'Failed!'
end if
write (*, '(A)') 'Test 3:'
if (.not. has_duplicate_value_within_k(nums3, k3)) then
write (*, '(A)') 'Passed!'
else
write (*, '(A)') 'Failed!'
end if
end program test_has_duplicate_value_within_k
temp.f95:4:42: 4 | public :: has_duplicate_value_within_k | 1 Error: PUBLIC attribute applied to MODULE has_duplicate_value_within_k at (1) temp.f95:8:49: 8 | logical function has_duplicate_value_within_k(nums, k) result(has_duplicate) | 1 Error: MODULE attribute of ‘has_duplicate_value_within_k’ conflicts with PROCEDURE attribute at (1) temp.f95:9:38: 9 | integer, intent(in) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:32: 10 | integer, intent(in) :: k | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:11:23: 11 | integer :: i, j | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:13:31: 13 | has_duplicate = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:15:32: 15 | do i = 1, size(nums) - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:16:36: 16 | do j = i + 1, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:17:66: 17 | if (nums(i) == nums(j) .and. abs(i - j) <= k) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:18:42: 18 | has_duplicate = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:19:24: 19 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:20:19: 20 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:21:15: 21 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:22:35: 22 | if (has_duplicate) exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:23:11: 23 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:24:7: 24 | end function has_duplicate_value_within_k | 1 Error: Expecting END MODULE statement at (1) temp.f95:28:9: 28 | use has_duplicate_value_within_k | 1 Fatal Error: Cannot open module file ‘has_duplicate_value_within_k.mod’ for reading at (1): No such file or directory compilation terminated.
def containsNearbyDuplicate(nums, k):
value_index_map = {}
for i, num in enumerate(nums):
if num in value_index_map and i - value_index_map[num] <= k:
return True
value_index_map[num] = i
return False
The algorithm utilizes a Hashtable, in which we store the integer value present in the array as the key and its latest index position as the value. For each element in the array, we first check if the current number is already in the Hashtable. If it is, we also check if the difference between the current index and the latest index stored in the Hashtable is less than or equal to k. If both conditions are met, we return true
because there are two repeating integers having an index difference less than or equal to k. Otherwise, we add the number along with its latest index to the Hashtable and continue with the next element. If the loop finishes without finding such a pair, we return false
.
Note: The algorithm's time complexity is O(n), where n is the length of the input array, and the space complexity is O(min(n, k)).
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map<int, int> valueIndexMap;
for (int i = 0; i < nums.size(); i++) {
if (valueIndexMap.find(nums[i]) != valueIndexMap.end() && i - valueIndexMap[nums[i]] <= k) {
return true;
}
valueIndexMap[nums[i]] = i;
}
return false;
}
The algorithm utilizes a Hashtable, in which we store the integer value present in the array as the key and its latest index position as the value. For each element in the array, we first check if the current number is already in the Hashtable. If it is, we also check if the difference between the current index and the latest index stored in the Hashtable is less than or equal to k. If both conditions are met, we return true
because there are two repeating integers having an index difference less than or equal to k. Otherwise, we add the number along with its latest index to the Hashtable and continue with the next element. If the loop finishes without finding such a pair, we return false
.
Note: The algorithm's time complexity is O(n), where n is the length of the input array, and the space complexity is O(min(n, k)).