-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_duplicate.cc
46 lines (32 loc) · 1.33 KB
/
array_duplicate.cc
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
/*
Problem description:
---------------------
You have a read-only array A[1..n] which is populated by numbers from 1..n-1, which implies atleast one repetition. However, there can be more. Find any one repeated number in linear time using constant space.
Solution:
-----------
As the A[n] =‚ n (The array contains elements between 1 c n-1) then A[n] contains some other element from 1 to n-1. Now starting from A[n] you can think of a linked list. Assume that A[n] points to k if A[n] = k. The linked list starting from A[n] must have a loop at the end as there are duplicate elements.
Proof: If there is no loop. Then there are no two nodes pointing to the same node. In that case the linked list starting from A[n] will be of length n and all n node will contain different values. Which is not true.
Now it just boils down to cycle detection algorithm in a linked list. You need to find out the start node of the cycle.
*/
#include <iostream>
#define SIZE sizeof(input)/sizeof(input[0])
int findDuplicate(const int *A, int n)
{
int x = n, y = n;
do {
x = A[A[x]];
y = A[y];
} while (x!=y);
x = n;
do {
x = A[x];
y = A[y];
} while (x!=y);
return x;
}
int main()
{
int input[] = {3,3,4,4,1,2};
std::cout << "Duplicate no: " << findDuplicate(input, SIZE-1) << std::endl;
return 0;
}