#K65667. Contains Nearby Almost Duplicate
Contains Nearby Almost Duplicate
Contains Nearby Almost Duplicate
Given an unsorted array of integers and an integer k, determine whether there exist two distinct indices i and j in the array such that:
$$|nums[i]-nums[j]| \le k \quad \text{and} \quad |i-j| \le k.$$
If such a pair exists, output true
; otherwise, output false
.
Note: The input array and the parameter k are provided through standard input.
inputFormat
The first line of input contains two integers n and k, where n is the number of elements in the array, and k is the threshold value used for both index distance and value difference.
The second line contains n space-separated integers representing the array nums
.
outputFormat
Output a single line to standard output. Print true
if there exist two distinct indices i and j such that |nums[i]-nums[j]| ≤ k
and |i-j| ≤ k
; otherwise, print false
.
4 3
1 2 3 1
true