-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContainsDuplicateII.java
45 lines (40 loc) · 1.31 KB
/
ContainsDuplicateII.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
/*
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.
*/
import java.io.*;
import java.util.*;
public class ContainsDuplicateII {
public static boolean containsNearbyDuplicate(int[] nums, int k) {
if(k <=0) return false;
if(nums.length <= 1) return false;
HashSet<Integer> hs = new HashSet<Integer>();
int l = 0;
for(int i = 0; i < nums.length; i++){
if(!hs.add(nums[i])) return true;
if(l < k){
l++;
}else{
hs.remove(nums[i-k]);
}
}
return false;
}
public static void dispAr(int[] nums) {
for(int i = 0;i < nums.length; i++){
System.out.print(nums[i] + " ");
}
System.out.println();
return;
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println("Enter a number: ");
int n = reader.nextInt();
int[] nums = new int[]{1, 2, 5, 8, 7, 3, 5, 0};
boolean res = containsNearbyDuplicate(nums, n);
System.out.println("array:");
dispAr(nums);
System.out.println("contains duplicate within " + n + "? " + res);
return;
}
}