-
Notifications
You must be signed in to change notification settings - Fork 277
/
FindTheDuplicateNumber.java
55 lines (41 loc) · 1.25 KB
/
FindTheDuplicateNumber.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
// @saorav21994
// TC : O(n)
// SC : O(1)
// My original solution. Use -ve index marking and revert back to preserve original array.
class Solution {
public int findDuplicate(int[] nums) {
int mark = -1;
for (int i = 0; i < nums.length; i++) {
int cur = Math.abs(nums[i]);
if (nums[cur] < 0) {
mark = cur;
break;
}
nums[cur] *= -1;
}
// re-store the original array
for (int i = 0; i < nums.length; i++) {
nums[i] = Math.abs(nums[i]);
}
return mark;
}
}
// Slow-Fast pointer approach taken from LeetCode solutions. Same as we find in Linked List detect cycle problem.
// TC : O(n)
// SC : O(1), No modification to array whatsoever
class Solution {
public int findDuplicate(int[] nums) {
int slow = nums[0];
int fast = nums[0];
do {
slow = nums[slow];
fast = nums[nums[fast]];
} while (slow != fast); // do-while loop to take care of indices
slow = nums[0];
while (slow != fast) {
fast = nums[fast];
slow = nums[slow];
}
return slow;
}
}