-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashing_Example.java
62 lines (51 loc) · 1.25 KB
/
Hashing_Example.java
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;
/**
*
* @author pulkit4tech
*/
public class Hashing_Example implements Runnable {
BufferedReader c;
PrintWriter pout;
// static long mod = 1000000007;
public void run() {
try {
c = new BufferedReader(new InputStreamReader(System.in));
pout = new PrintWriter(System.out, true);
solve();
pout.close();
} catch (Exception e) {
pout.close();
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) throws Exception {
new Thread(new Hashing_Example()).start();
}
void solve() throws Exception {
check_duplicate_within_K();
}
void check_duplicate_within_K(){
int arr[] = {10, 5, 3, 4, 3, 5, 6};
if (checkDuplicatesWithinK(arr, 3))
pout.println("Yes");
else
pout.println("No");
}
boolean checkDuplicatesWithinK(int arr[], int k)
{
HashSet<Integer> set = new HashSet<>();
for (int i=0; i<arr.length; i++)
{
if (set.contains(arr[i]))
return true;
set.add(arr[i]);
if (i >= k)
set.remove(arr[i-k]);
}
return false;
}
}