Refs:-
Floydβs Cycle Detection & Duplicate Finding Algorithm (Tortoise and Hare)
Sibling A problems:-
PKM Note: Array-to-Graph Mappings (Elements as Indices)
π Core Concept: Arrays as Functional Graphs
- The Paradigm Shift: When an array of size contains integers strictly bounded within a fixed range (usually or ), the array can be modeled as a Directed Functional Graph.
- The Mapping Rule: Each index represents a graph node. The value sitting at that index, , represents a directed edge pointing to the next node: .
- Why It Matters: This structural insight unlocks time and auxiliary space complexities. It bypasses the need for traditional
HashSetsorHashMapsby reusing the arrayβs own memory infrastructure.
ββββββββββββββββββββββββββββββββββββββββββ
β Domain: Arrays as Functional Graphs β
β (Values Map to Indices) β
βββββββββββββββββββββ¬βββββββββββββββββββββ
β
βββββββββββββββββββββ΄βββββββββββββββββββββ
β β
βββββββββββββββΌββββββββββββββ βββββββββββββββΌββββββββββββββ
β Sibling A: In-Place State β β Sibling B: Floyd's Cycle β
β Mutation & Cyclic Sort β β Detection β
βββββββββββββββββββββββββββββ βββββββββββββββββββββββββββββ
πΏ Sibling A: In-Place State Mutation & Cyclic Sort
This branch treats the array as a mutable map where you aggressively mark states or physically swap elements into their mathematically correct addresses.
Sub-Pattern 1: Sign Flipping (Implicit State Marking)
-
Mechanism: Iterate through the array. For every value , treat as a target index. Flip the number residing at that target index to a negative sign (
nums[target] *= -1). -
Significance: The sign acts as a 1-bit boolean flag (βVisitedβ). A positive value remaining at the end indicates that its corresponding index was never visited.
-
Example:
nums = [2, 1, 1](Size 3)- value is
2. Target index = . Flipnums[1]. - value is
|-1| = 1. Target index = . Flipnums[0]. - value is
1. Target index = .nums[0]is already negative! π₯ Duplicate found: 1.
- value is
-
Target Problems:
- LeetCode 442: Find All Duplicates in an Array (Meta, Amazon)
- LeetCode 448: Find All Numbers Disappeared in an Array (Amazon, Google)
Sub-Pattern 2: Cyclic Sort (In-Place Swapping)
-
Mechanism: Actively swap elements until every valid positive number sits at its βhome indexβ ( or ). Ignore elements out of the boundary range.
-
Example:
nums = [3, 4, -1, 1](Size 4)- Index 0 holds
3. It belongs at index3-1 = 2. Swap withnums[2]. Array:[-1, 4, 3, 1]. - Index 0 holds
-1. Out of bounds, skip. - Index 1 holds
4. Belongs at index4-1 = 3. Swap withnums[3]. Array:[-1, 1, 3, 4]. - Index 1 holds
1. Belongs at index1-1 = 0. Swap withnums[0]. Array:[1, -1, 3, 4]. - Final check scan: Index 1 contains
-1instead of2. π₯ First missing positive is 2.
- Index 0 holds
-
Target Problems:
- LeetCode 41: First Missing Positive (Meta, Google, Uber)
- LeetCode 268: Missing Number (Microsoft, Apple)
- LeetCode 645: Set Mismatch (TikTok)
πΏ Sibling B: Floydβs Cycle Detection (Tortoise & Hare)
This branch views the index-to-value relationship as an unmodifiable pointer trail. It uses two pointers tracking through the implicit graph nodes at different speeds to identify cycles and cycle entry points without altering any data.
-
Mechanism:
- Phase 1: Move
slowby one step (slow = nums[slow]) andfastby two steps (fast = nums[nums[fast]]). If they collide, a cycle exists. - Phase 2: Reset
slowto the graph origin. Move bothslowandfastat a uniform speed of one step at a time. The precise node where they meet again is the mathematical entrance to the cycle.
- Phase 1: Move
-
Example:
nums = [1, 3, 4, 2, 2](Indices: 0 to 4)-
Graph edges: (Note the cycle loop between indices 2, 4, and 3).
-
Phase 1: Pointers advance through indices. They collide at index 4.
-
Phase 2: Reset
slowto 0. Move both 1 step at a time.slowgoesfastgoes
-
Collision occurs at index
2. π₯ Duplicate found: 2.
-
-
Target Problems:
- LeetCode 287: Find the Duplicate Number (Netflix, Amazon, Google) β The classic array-to-Floyd conversion.
- LeetCode 142: Linked List Cycle II (Microsoft, Uber)
- LeetCode 202: Happy Number (Netflix, Meta) β Implicit function-driven state cycle.
βοΈ Summary Strategy: How to Choose?
| Scenario Requirement | Use Sibling A (Mutation/Cyclic Sort) | Use Sibling B (Floydβs Algorithm) |
|---|---|---|
| Array Mutability | Read-Write allowed (can flip signs or swap elements). | Strictly Read-Only array constraints. |
| Problem Type | Finding multiple missing or multiple duplicate items. | Finding exactly one cycle entry point or duplicate. |
| Data Conditions | Elements can be outside the range (ignored). | All elements must form safe, valid index jumps. |