Skip to content

Commit eecbf68

Browse files
committed
added Find the Duplicate Number
1 parent db1dfee commit eecbf68

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package Medium.FindDuplicateNumber;
2+
3+
import java.util.Arrays;
4+
5+
public class Driver {
6+
public static void main(String[] args) {
7+
Solution sol = new Solution();
8+
int[] arr = {1,3,4,2,2};
9+
System.out.println("Input: " + Arrays.toString(arr));
10+
int dup = sol.findDuplicate(arr);
11+
System.out.println("Output: " + dup);
12+
13+
System.out.println();
14+
int[] arr2 = {3,1,3,4,2};
15+
System.out.println("Input: " + Arrays.toString(arr2));
16+
int dup2 = sol.findDuplicate(arr2);
17+
System.out.println("Output: " + dup2);
18+
}
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
# Find the Duplicate Number
3+
[Leetcode Link](https://leetcode.com/problems/find-the-duplicate-number/)
4+
5+
## Problem:
6+
7+
Given an array nums containing `n + 1` integers where each integer is between `1` and `n` (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
8+
9+
## Example:
10+
11+
```
12+
Input: [1,3,4,2,2]
13+
Output: 2
14+
```
15+
```
16+
Input: [3,1,3,4,2]
17+
Output: 3
18+
```
19+
20+
## Note:
21+
22+
1. You **must not** modify the array (assume the array is read only).
23+
2. You must use only constant, O(1) extra space.
24+
3. Your runtime complexity should be less than O(n^2).
25+
4. There is only one duplicate number in the array, but it could be repeated more than once.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package Medium.FindDuplicateNumber;
2+
3+
// Use Floyd's Tortoise and Hare Algorithm to detect cycle's entry point
4+
// https://leetcode.com/problems/find-the-duplicate-number/solution/
5+
// See approach 3 for detailed explanation.
6+
// Key ideas
7+
// Two phases:
8+
// Phase 1: Hare runs double speed as tortoise, and where they meet is intersection in the cycle.
9+
// Phase 2: Tortoise start from beginning, hare runs at tortoise's pace, where they meet again is the cycle's entry point.
10+
11+
class Solution {
12+
public int findDuplicate(int[] nums) {
13+
int slow = nums[nums[0]],
14+
fast = nums[slow];
15+
while (slow != fast) {
16+
slow = nums[slow];
17+
fast = nums[nums[fast]];
18+
}
19+
// move slower traversal back to starting
20+
slow = nums[0];
21+
while (slow != fast) {
22+
slow = nums[slow];
23+
fast = nums[fast];
24+
}
25+
return slow;
26+
}
27+
}

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ Languages used: Java and Python
1515
- [Intersection of Two Arrays](Easy/IntersectionOfTwoArrays)
1616
- Medium
1717
- [Minimum Add to Make Parentheses Valid](Medium/MinimumAddtoMakeParenthesesValid)
18-
- [Distribute Coins in Binary Tree](#Medium/DistributionCoinsInBinaryTree)
19-
- [Find Minimum Number of Fibonacci Numbers Whose Sum is K](#Medium/FindMinNumFibNumSumK)
18+
- [Distribute Coins in Binary Tree](Medium/DistributionCoinsInBinaryTree)
19+
- [Find Minimum Number of Fibonacci Numbers Whose Sum is K](Medium/FindMinNumFibNumSumK)
20+
- [Find the Duplicate Number](Medium/FindDuplicateNumber)
2021
- Hard
2122

2223
---

0 commit comments

Comments
 (0)