Arun Pandian M

Arun Pandian M

Android Dev | Full-Stack & AI Learner

Detecting Cycles & Repeats: Fast & Slow Pointer Pattern

If you’ve ever watched two runners on a track, one sprinting and the other jogging, you know that sometimes the faster runner will eventually catch up to the slower one. This is exactly the intuition behind the fast & slow pointer pattern, used to detect cycles or repeated elements in arrays and linked structures efficiently.

Pattern Definition

The Fast & Slow Pointer Pattern involves two pointers moving at different speeds through a sequence. Typically, the fast pointer moves twice as fast as the slow pointer, and their meeting point indicates a cycle or repetition.

Think of it as two cars driving on a circular track: the faster car eventually laps the slower one. The moment they meet tells you something important about the track or the loop.

Problem Example: Find the Duplicate Number in Array

Sample Input: [3, 1, 3, 4, 2]

Goal: Find the number that appears twice without modifying the array or using extra space.

Real-Life Analogy: Imagine a conveyor belt in a factory with packages. One package repeats itself due to a misrouting. You want to detect the repeated package without scanning every slot multiple times.

https://storage.googleapis.com/lambdabricks-cd393.firebasestorage.app/cycle_detection_two_pointer_aaray.svg?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=firebase-adminsdk-fbsvc%40lambdabricks-cd393.iam.gserviceaccount.com%2F20260117%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20260117T151050Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=63d50615476c5db60aeb5cdc8eacd11186e17ebcece236a99df2249340c0ba4e22afb9478ba115303ae0f79eb9bd02b1e8ae09aab1e687cde48f3d5a5ab15d5c483aab89897c579636cb76b587c396443ff96b6cbd91e92b05e2749e09e72d4e20afe4a697634a8e96682384be3f722e8a760aab4bcdf21e317769886ed2d796977a32504b74d3ff12f8f9c221eb567b10a772bff3a9723cc4d85d9a49d81e2d2b6242b6d5e6035f151888cec8dea358f3ced54845aaa4b8be8d96d40d60b09857c4bd67f67ff5c5b85566b7ea79fa3a4a5d3a9c9956db22a7a2025c8ab3cf391bdb5a942f17afde01d47b7308dc5e682ad4c26118acc8ce3a555714af2c2083

Flow (Step-by-Step)

Start
  |
  v
Initialize:
  slow = nums[0] (slow runner)
  fast = nums[nums[0]] (fast runner)
  |
  v
Move:
  - slow moves 1 step: slow = nums[slow]
  - fast moves 2 steps: fast = nums[nums[fast]]
  |
  v
Check:
  - If slow == fast -> meeting point found
  - Else -> repeat moving pointers
  |
  v
Reset fast to start (fast = 0)
  |
  v
Move both 1 step at a time until slow == fast
  |
  v
Duplicate number found = slow

Kotlin Solution

fun findDuplicate(nums: IntArray): Int {
    var slow = nums[0]
    var fast = nums[nums[0]]

    while (slow != fast) {
        slow = nums[slow]
        fast = nums[nums[fast]]
    }

    fast = 0
    while (slow != fast) {
        slow = nums[slow]
        fast = nums[fast]
    }

    return slow
}

fun main() {
    val nums = intArrayOf(3, 1, 3, 4, 2)
    println(findDuplicate(nums)) // Output: 3
}

Time & Space Complexity

Time Complexity: O(n) Each pointer moves linearly. Even though the fast pointer moves twice as fast, it will meet the slow pointer within a single pass around the cycle.

Space Complexity: O(1) No extra storage is needed beyond the two pointers, making it space-efficient.

Real-Time Use Cases

1. Network Packet Loops: Detect repeated routing or infinite loops.

2. User Behavior Tracking: Identify repeating user actions for fraud detection.

3. Linked Lists & Memory Checks: Detect cycles in linked data.

4. Games & Animations: Detect collisions or overlaps on circular paths.

5. Event Scheduling: Detect repeated states in recurring sensor signals.

Conclusion

The fast & slow pointer pattern is a powerful, efficient tool for detecting cycles or duplicates without extra memory. It leverages speed differences to pinpoint repetitions in linear time.

Next Pattern: Expanding Around Center

Once you’ve mastered converging and racing pointers, the next trick is Expanding Around Center. Imagine checking for palindromes in a string: start from the middle and expand outward, capturing symmetric patterns. This technique is perfect for problems like longest palindromic substring or mirror patterns in arrays, and it also builds upon the two-pointer intuition.

#algorithm_patterns#problem_solving#time_complexity#fast_slow_pointer#floyd_cycle_detection#cycle_detection#array_algorithms#linked_list_cycles#dsa_kotlin#algorithm_intuition#find_duplicate_number#pointers_in_dsa#two_pointer_technique#space_optimization#real_life_dsa