#C8548. Contains Nearby Duplicates
Contains Nearby Duplicates
Contains Nearby Duplicates
Given an array of integers and an integer \( k \), determine whether there exists a pair of duplicate elements such that the absolute difference between their indices is at most \( k \). In other words, given an array \( nums \), check if there exist two indices \( i \) and \( j \) satisfying:
[ |i - j| \le k ]
and \( nums[i] = nums[j] \). If such a pair exists, output YES
; otherwise, output NO
.
Note: The input is provided via standard input and the result should be printed to standard output.
inputFormat
The first line contains two integers \( n \) and \( k \) separated by a space, where \( n \) is the number of elements in the array and \( k \) is the maximum allowed index distance for duplicates.
The second line contains \( n \) space-separated integers representing the array \( nums \).
outputFormat
Output a single line: YES
if there exists at least one pair of duplicate elements within a distance of \( k \); otherwise, output NO
.
6 3
1 2 3 1 2 3
YES