115. Duplicate Within K Positions
Core1000 ms256 MBSolved by 0%
Print Yes if the array contains two equal values whose positions are at most
k apart, or No otherwise.
Positions i and j are at most k apart when |i − j| ≤ k.
Constraints - `1 ≤ n ≤ 200000` - `0 ≤ k ≤ n` - `-1000000000 ≤ a[i] ≤ 1000000000`
Input
The first line contains two integers n and k.
The second line contains n space-separated integers.
Output
Print exactly one word: Yes or No.
Input5 2
1 2 3 1 5
OutputNo
Notehas a repeat exactly three positions apart, one further than allowed
Input5 3
1 2 3 1 5
OutputYes
Noteis the same array with k raised by one, so the repeat now qualifies
Hint 1Approach
Hint 2Approach
Hint 3Pseudocode
Hint 4Full solution