Skip to content

Commit a3c40fe

Browse files
SwastyyvbrazoPanquesito7
authored
Explanation of Floyd Cycle Detection Algo (TheAlgorithms#122)
* update * Update en/Search Algorithms/Floyd Cycle Detection Algorithm to Find Duplicate Number.md Co-authored-by: David Leal <[email protected]> * Update en/Search Algorithms/Floyd Cycle Detection Algorithm to Find Duplicate Number.md Co-authored-by: David Leal <[email protected]> * Update en/Search Algorithms/Floyd Cycle Detection Algorithm to Find Duplicate Number.md Co-authored-by: David Leal <[email protected]> * Update en/Search Algorithms/Floyd Cycle Detection Algorithm to Find Duplicate Number.md Co-authored-by: David Leal <[email protected]> * Update en/Search Algorithms/Floyd Cycle Detection Algorithm to Find Duplicate Number.md Co-authored-by: David Leal <[email protected]> * Update en/Search Algorithms/Floyd Cycle Detection Algorithm to Find Duplicate Number.md Co-authored-by: David Leal <[email protected]> * Update en/Search Algorithms/Floyd Cycle Detection Algorithm to Find Duplicate Number.md Co-authored-by: David Leal <[email protected]> * Update en/Search Algorithms/Floyd Cycle Detection Algorithm to Find Duplicate Number.md Co-authored-by: David Leal <[email protected]> * Update en/Search Algorithms/Floyd Cycle Detection Algorithm to Find Duplicate Number.md Co-authored-by: David Leal <[email protected]> * Update en/Search Algorithms/Floyd Cycle Detection Algorithm to Find Duplicate Number.md Co-authored-by: David Leal <[email protected]> * Update en/Search Algorithms/Floyd Cycle Detection Algorithm to Find Duplicate Number.md * minor changes Co-authored-by: Vitor Oliveira <[email protected]> Co-authored-by: David Leal <[email protected]>
1 parent 0175ecd commit a3c40fe

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Floyd Cycle Detection Algorithm to find duplicate number in an array
2+
3+
## Problem Statement
4+
5+
Given an array of integers containing `n + 1` integers, where each integer is in the range `[1, n]` inclusive. If there is only one duplicate number in the input array, this algorithm returns the duplicate number without modifying the original array, otherwise, it returns -1.
6+
7+
## Approach
8+
9+
- Use the function `f(x) = arr[x]` to construct the sequence:
10+
`arr[0]`, `arr[arr[0]]`, `arr[arr[arr[0]]]`, `arr[arr[arr[arr[0]]]]`, etc....
11+
- Each new element in the sequence is an element in `arr[]` at the index of the previous element.
12+
- Starting from `x = arr[0]`, it will produce a linked list with a cycle.
13+
- The cycle appears because `arr[]` contains duplicate elements(at least one). The duplicate value is an entrance to the cycle.
14+
15+
## Time Complexity
16+
17+
O(n)
18+
19+
## Space Complexity
20+
21+
O(1)
22+
23+
## Example
24+
25+
```
26+
arr = [3, 4, 8, 5, 9, 1, 2, 6, 7, 4]
27+
28+
return value = 4
29+
```
30+
31+
## Code Implementation Links
32+
33+
- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/search/floyd_cycle_detection_algo.cpp)
34+
- [C](https://github.com/TheAlgorithms/C/blob/master/searching/floyd_cycle_detection_algorithm.c)
35+
36+
#### Video Explanation
37+
38+
[YouTube video explaining the Floyd Cycle Detection algorithm](https://www.youtube.com/watch?v=B6smdk7pZ14)

0 commit comments

Comments
 (0)