Skip to content

Commit 14db620

Browse files
authored
Add files via upload
1 parent 957565a commit 14db620

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

FindDuplicateNumber.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//https://leetcode.com/problems/find-the-duplicate-number/
2+
//Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.
3+
//
4+
//There is only one repeated number in nums, return this repeated number.
5+
//
6+
//You must solve the problem without modifying the array nums and uses only constant extra space.
7+
//All the integers in nums appear only once except for precisely one integer which appears two or more times.
8+
//Input: nums = [3,1,3,4,2]
9+
//Output: 3
10+
//Input: nums = [1,3,2,2,2]
11+
// Output: 2
12+
public class FindDuplicateNumber {
13+
14+
15+
16+
//When all numbers are unique we can traverse whole array as each element is independent and takes to unique place
17+
// when there is a duplicate value if forms a circle which would be the beginning of cyclic linkedlist
18+
//we need to find the begining of cycle in circular linked list
19+
20+
//intially use fst and slw ptrs to meet inside the circle
21+
22+
// if slw ptr moves N distance
23+
// fst ptr moves 2N distance
24+
// let distance from head to beginning of circle is d and point they met is k from beginning of circle and made i circles to meet (i)C
25+
// for fst ptr 2N = D + k + (j)C
26+
// for slw ptr N = D + k + (i)C
27+
// on subtraction N = (j-i)C
28+
// substitutin in slw ptr equation (j-i)C = D + k + (i)C
29+
//(j-2i)C = D + k ===> D+k is multiple of C (C is circular list length) which means if we travel D+k distance we complete the whole circle
30+
31+
32+
// if we strat from beginning and move both ptrs by 1
33+
//travelling D distance by fst ptr and slw ptr will travel D distance inside the circle but to complete the circle it has to travel k more to reach its position which means it is short by k to be a k point from beginning of the circle, hence they meet at starting of the circular linked list
34+
35+
public int findDuplicate(int[] nums) {
36+
int p1=nums[0],p2=nums[nums[0]];
37+
//find the meeting point i.e find k (point of meeting)
38+
while(nums[p1]!=nums[p2]) {
39+
p1=nums[p1];
40+
p2=nums[nums[p2]];
41+
}
42+
43+
p2=0;
44+
45+
//find the starting point of the circle
46+
while(nums[p1]!=nums[p2]) {
47+
p1=nums[p1];
48+
p2=nums[p2];
49+
}
50+
51+
52+
return nums[p1];
53+
}
54+
55+
}
56+
57+
58+
// //for only 2 numbers duplicate
59+
// public int findDuplicate(int[] nums) {
60+
// //As we are looking for 1 to n numbers but nums contain n+1 values
61+
// // we start xor from 0 to n for complete array then xor 0 to remove
62+
// //As res is assigned with 0 xor again with 0
63+
// int res=0;
64+
// for(int i=0;i<nums.length;i++) {
65+
// res^=nums[i]^i;
66+
// }
67+
// return res^0^0;
68+
// }

0 commit comments

Comments
 (0)