- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstRepeatingSpecial.java
More file actions
Latest commit
27 lines (24 loc) · 868 Bytes
/
Copy pathFirstRepeatingSpecial.java
File metadata and controls
27 lines (24 loc) · 868 Bytes
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
//Good Question
//https://www.interviewbit.com/problems/find-duplicate-in-array/
//Given a read only array of n + 1 integers between 1 and n, find one number that repeats in linear time using less than O(n) space and traversing the stream sequentially O(1) times.
publicclassSolution {
// DO NOT MODIFY THE LIST
publicintrepeatedNumber(finalList<Integer> a) {
intsize = a.size();
intr = (int)Math.sqrt(size);
intb = ((size-1)/r);
int[][] arr = newint[r+1][];
intcounts[] = newint[r+1];
for(Integeri : a){
intpos = i%b == 0 ? (i/b - 1) : (i/b);
if(arr[pos] == null){
arr[pos] = newint[r+1];
}
for(intj = 0; j < counts[pos]; j++){
if(i == arr[pos][j]) returni;
}
arr[pos][counts[pos]++] = i;
}
return -1;
}
}