#K60337. Duplicate Elements Within K Distance
Duplicate Elements Within K Distance
Duplicate Elements Within K Distance
Given an array \(A\) of \(n\) integers and an integer \(k\), your task is to determine whether there exist two identical elements in the array whose indices differ by at most \(k\). In other words, check if there exist indices \(i\) and \(j\) such that \(|i - j| \le k\) and \(A[i] = A[j]\). If such a pair exists, output True
; otherwise, output False
.
inputFormat
The input is given from standard input (stdin) and consists of three lines:
- The first line contains a single integer \(n\) which is the number of elements in the array.
- The second line contains \(n\) space-separated integers, representing the elements of the array \(A\).
- The third line contains a single integer \(k\), the maximum allowed distance between duplicate elements.
outputFormat
Output a single line to standard output (stdout): True
if there is at least one pair of duplicate elements within a distance of \(k\), and False
otherwise.
4
1 2 3 1
3
True