#K78037. Contains Nearby Duplicate
Contains Nearby Duplicate
Contains Nearby Duplicate
You are given an array of integers and an integer k. Your task is to determine if there exist two distinct indices i and j such that a[i] = a[j] and the absolute difference between i and j is at most k. In other words, find out whether the condition
[ |i - j| \le k \quad \text{and} \quad a[i] = a[j] ]
holds for at least one pair of indices in the array.
Example: For the array [1, 0, 1, 1] with k = 1, since a[2] equals a[3] and \(|2 - 3| = 1 \leq 1\), the output should be True
.
inputFormat
The input is given via standard input (stdin) and has the following format:
- The first line contains two space-separated integers
n
andk
, wheren
is the number of elements in the array. - The second line contains
n
space-separated integers representing the array.
It is guaranteed that n ≥ 1
and k ≥ 0
.
outputFormat
Output a single line to standard output (stdout) containing True
if the array contains at least one pair of equal elements such that the absolute difference between their indices is at most k, otherwise output False
.
6 2
1 2 3 1 2 3
False