|
Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
Implementation of Floyd's Cycle Detection algorithm. More...
#include <cassert>#include <iostream>#include <vector>Namespaces | |
| namespace | search |
| for std::vector | |
| namespace | cycle_detection |
| Functions for the Floyd's Cycle Detection algorithm. | |
Functions | |
| template<typename T > | |
| int32_t | search::cycle_detection::duplicateNumber (const std::vector< T > &in_arr, const uint32_t &n) |
| The main function implements search algorithm. More... | |
| static void | test () |
| Self-test implementations. More... | |
| int | main () |
| Main function. More... | |
Implementation of Floyd's Cycle Detection algorithm.
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 in O(1) space and the time complexity is less than O(n^2) without modifying the original array, otherwise, it returns -1.
| int32_t search::cycle_detection::duplicateNumber | ( | const std::vector< T > & | in_arr, |
| const uint32_t & | n | ||
| ) |
The main function implements search algorithm.
| T | type of array |
| in_arr | the input array |
| n | size of array |
| int main | ( | void | ) |
|
static |
Self-test implementations.