TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Implementation of Floyd's Cycle Detection algorithm. More...
#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>
Go to the source code of this file.
Namespaces | |
namespace | search |
for std::assert | |
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. | |
static void | test () |
Self-test implementations. | |
int | main () |
Main function. | |
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.
Definition in file floyd_cycle_detection_algo.cpp.
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 |
Definition at line 37 of file floyd_cycle_detection_algo.cpp.
int main | ( | void | ) |
Main function.
Definition at line 95 of file floyd_cycle_detection_algo.cpp.
|
static |
Self-test implementations.
Definition at line 64 of file floyd_cycle_detection_algo.cpp.