-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFindDuplicateNumber.java
35 lines (31 loc) · 990 Bytes
/
FindDuplicateNumber.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
/*
Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.
There is only one repeated number in nums, return this repeated number.
You must solve the problem without modifying the array nums and uses only constant extra space.
Follow up:
How can we prove that at least one duplicate number must exist in nums?
Can you solve the problem in linear runtime complexity?
*/
public class FindDuplicateNumber {
public static void main(String[] args) {
int[] nums = {1,2,3,4,2};
System.out.println(findDuplicate(nums));
}
public static int findDuplicate(int[] nums) {
int slow = 0, fast = 0;
do{
slow = nums[slow];
fast = nums[nums[fast]];
}while(slow != fast);
slow = 0;
while(slow != fast){
slow = nums[slow];
fast = nums[fast];
}
return slow;
}
}
/*
Time complexity: O(n)
Space Complexity: O(1)
*/