Category: fast_and_slow_pointer
-
LeetCode 287: Find the Duplicate Number
link Here values in nums is a permutation of the indices. If we consider i and nums[i] as consecutive nodes of a linked list, we have a permutation of the indices in the linked list. Say nums[i] = 2 and nums[j] = 2. This creates a cycle. With fast and slow pointers we can find…
-
LeetCode 457: Circular Array Loop
link Since the array is circular, there is always a cycle. A loop is: For example, below, if we start from index 0, the direction changes at the hop . But within the cycle: , the direction does not change. So, we declare it has a loop. Starting from each index we can check the…
-
LeetCode 202: Happy Number
link We run until we see a repeated number and check if that is 1. Say is an digit number. So, . Let . The maximum value of is when digits are all ‘s. In other words, the maximum value of . Since , . So, one step of sum of digit squared reduces to…