|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief Implementation of [Floyd's Cycle |
| 4 | + * Detection](https://en.wikipedia.org/wiki/Cycle_detection) algorithm |
| 5 | + * @details |
| 6 | + * Given an array of integers containing 'n + 1' integers, where each |
| 7 | + * integer is in the range [1, n] inclusive. If there is only one duplicate |
| 8 | + * number in the input array, this algorithm returns the duplicate number in |
| 9 | + * O(1) space and the time complexity is less than O(n^2) without modifying the |
| 10 | + * original array, otherwise, it returns -1. |
| 11 | + * @author [Swastika Gupta](https://github.com/Swastyy) |
| 12 | + */ |
| 13 | + |
| 14 | +#include <cassert> /// for assert |
| 15 | +#include <iostream> /// for IO operations |
| 16 | +#include <vector> /// for std::vector |
| 17 | + |
| 18 | +/** |
| 19 | + * @namespace search |
| 20 | + * @brief Search algorithms |
| 21 | + */ |
| 22 | +namespace search { |
| 23 | +/** |
| 24 | + * @namespace cycle_detection |
| 25 | + * @brief Functions for the [Floyd's Cycle |
| 26 | + * Detection](https://en.wikipedia.org/wiki/Cycle_detection) algorithm |
| 27 | + */ |
| 28 | +namespace cycle_detection { |
| 29 | +/** |
| 30 | + * @brief The main function implements search algorithm |
| 31 | + * @tparam T type of array |
| 32 | + * @param in_arr the input array |
| 33 | + * @param n size of array |
| 34 | + * @returns the duplicate number |
| 35 | + */ |
| 36 | +template <typename T> |
| 37 | +int32_t duplicateNumber(const std::vector<T> &in_arr, const uint32_t &n) { |
| 38 | + if (n == 0 || n == 1) { // to find duplicate in an array its size should be atleast 2 |
| 39 | + return -1; |
| 40 | + } |
| 41 | + uint32_t tortoise = in_arr[0]; // variable tortoise is used for the longer |
| 42 | + // jumps in the array |
| 43 | + uint32_t hare = in_arr[0]; // variable hare is used for shorter jumps in the array |
| 44 | + do { |
| 45 | + tortoise = in_arr[tortoise]; |
| 46 | + hare = in_arr[in_arr[hare]]; |
| 47 | + } while (tortoise != hare); |
| 48 | + tortoise = in_arr[0]; |
| 49 | + while (tortoise != hare) { |
| 50 | + tortoise = in_arr[tortoise]; |
| 51 | + hare = in_arr[hare]; |
| 52 | + } |
| 53 | + return tortoise; |
| 54 | +} |
| 55 | +} // namespace cycle_detection |
| 56 | +} // namespace search |
| 57 | + |
| 58 | +/** |
| 59 | + * @brief Self-test implementations |
| 60 | + * @returns void |
| 61 | + */ |
| 62 | +static void test() { |
| 63 | + // 1st test |
| 64 | + // [3, 4, 8, 5, 9, 1, 2, 6, 7, 4] return 4 |
| 65 | + std::vector<uint32_t> array1 = {3, 4, 8, 5, 9, 1, 2, 6, 7, 4}; |
| 66 | + std::cout << "Test 1... "; |
| 67 | + assert(search::cycle_detection::duplicateNumber(array1, array1.size()) == |
| 68 | + 4); // here the duplicate number is 4 |
| 69 | + std::cout << "passed" << std::endl; |
| 70 | + |
| 71 | + // 2nd test |
| 72 | + // [1, 2, 3, 4, 2] return 2 |
| 73 | + std::vector<uint32_t> array2 = {1, 2, 3, 4, 2}; |
| 74 | + std::cout << "Test 2... "; |
| 75 | + assert(search::cycle_detection::duplicateNumber(array2, array2.size()) == |
| 76 | + 2); // here the duplicate number is 2 |
| 77 | + std::cout << "passed" << std::endl; |
| 78 | + |
| 79 | + // 3rd test |
| 80 | + // [] return -1 |
| 81 | + std::vector<uint32_t> array3 = {}; |
| 82 | + std::cout << "Test 3... "; |
| 83 | + assert(search::cycle_detection::duplicateNumber(array3, array3.size()) == |
| 84 | + -1); // since the input array is empty no duplicate number exists in |
| 85 | + // this case |
| 86 | + std::cout << "passed" << std::endl; |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * @brief Main function |
| 91 | + * @returns 0 on exit |
| 92 | + */ |
| 93 | +int main() { |
| 94 | + test(); // run self-test implementations |
| 95 | + return 0; |
| 96 | +} |
0 commit comments