-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKth distance.py
42 lines (36 loc) · 994 Bytes
/
Kth distance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# User function Template for python3
class Solution:
def checkDuplicatesWithinK(self, arr, k):
if k <= 0 or not arr:
return False
seen = set()
for i in range(min(k, len(arr))):
if arr[i] in seen:
return True
seen.add(arr[i])
for i in range(k, len(arr)):
if arr[i] in seen:
return True
seen.add(arr[i])
seen.remove(arr[i - k])
return False
# {
# Driver Code Starts
# Initial Template for Python 3
# Position this line where user code will be pasted.
# Initial Template for Python 3
if name == "main":
t = int(input())
while t > 0:
arr = list(map(int, input().split()))
k = int(input())
ob = Solution()
res = ob.checkDuplicatesWithinK(arr, k)
if res:
print("true")
else:
print("false")
# print(res)
print("~")
t -= 1
# } Driver Code Ends