You are given an array of n integers and a non-negative integer k. Decide whether there exist two indices i < j such that the values are equal (nums[i] == nums[j]) and their positions are close, meaning j - i <= k.
Print YES if such a pair exists, otherwise print NO.
Line 1: two integers n and k.
Line 2: n space-separated integers, the array.
One line: YES or NO.
Example 1
Input
5 3 1 2 3 1 4
Expected
YES
Explanation
The value 1 sits at indices 0 and 3, a gap of 3, which is <= k = 3, so the answer is YES.
Example 2
Input
4 1 1 2 1 2
Expected
NO
Explanation
The repeated values 1 and 2 are each 2 apart, but k = 1, so no equal pair is close enough. The answer is NO.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →