-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduplicateInArray.java
71 lines (52 loc) · 1.39 KB
/
duplicateInArray.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
63
64
65
66
67
68
69
70
71
/*Problem Description
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.
If there are multiple possible answers ( like in the sample case ), output any one, if there is no duplicate, output -1
Problem Constraints
1 <= |A| <= 105
1 <= Ai <= |A|
Input Format
The first argument is an integer array A.
Output Format
Return the integer that repeats in array A
Example Input
Input 1:
A = [3, 4, 1, 4, 2]
Input 2:
A = [1, 2, 3]
Input 3:
A = [3, 4, 1, 4, 1]
Example Output
Output 1:
4
Output 2:
-1
Output 3:
1
Example Explanation
Explanation 1:
4 repeats itself in the array [3, 4, 1, 4, 2]
Explanation 2:
No number repeats itself in the array [1, 2, 3]
Explanation 3:
1 and 4 repeats itself in the array [3, 4, 1, 4, 1], we can return 1 or 4*/
//PROGRAM:
public class Solution {
// DO NOT MODIFY THE LIST. IT IS READ ONLY
public int repeatedNumber(final List<Integer> A) {
if(A.size()<=1){
return -1;
}
int slow =A.get(0);
int fast =A.get(A.get(0));
while(slow!=fast){
slow = A.get(slow);
fast = A.get(A.get(fast));
}
slow=0;
while(slow!=fast){
slow = A.get(slow);
fast = A.get(fast);
}
return slow;
}
}