Program to check we can reach end of list by starting from k in Python

Suppose we have a list of numbers called nums and another number k. If we start at index k and at any index i, we can go either left or right by exactly nums[i] number of steps. We have to check whether we can reach the end of the list or not.

So, if the input is like nums = [0, 0, 2, 1, 3, 3, 1, 1] and k = 2, then the output will be True, as if we start at index 2, then jump to index 4 and then jump to the last index 7.

Algorithm

To solve this, we will follow these steps −

  • n := size of nums

  • visited := a list of size n and fill with 0

  • tovisit := a list of size 1, and insert k into it

  • while size of tovisit > 0, do

    • i := last element from tovisit and delete it from tovisit

    • if i is same as n-1, then return True

    • if visited[i] is not same as 1, then

      • visited[i] := 1

      • up := i + nums[i]

      • down := i - nums[i]

      • if up , then insert up at the end of tovisit

      • if down >= 0, then insert down at the end of tovisit

  • return False

Example

Let us see the following implementation to get better understanding −

class Solution:
    def solve(self, nums, k):
        n = len(nums)
        visited = [0] * n
        tovisit = [k]
        
        while len(tovisit) > 0:
            i = tovisit.pop()
            if i == n - 1:
                return True
            
            if visited[i] != 1:
                visited[i] = 1
                up = i + nums[i]
                down = i - nums[i]
                
                if up < n:
                    tovisit.append(up)
                if down >= 0:
                    tovisit.append(down)
        
        return False

# Test the solution
ob = Solution()
nums = [0, 0, 2, 1, 3, 3, 1, 1]
k = 2
print(ob.solve(nums, k))
True

How It Works

The algorithm uses a breadth-first search (BFS) approach with a stack. Starting from index k, we explore all reachable positions by moving left or right based on the value at each index. We maintain a visited array to avoid revisiting the same index, preventing infinite loops.

In the example, starting at index 2 (value 2), we can jump to indices 0 and 4. From index 4 (value 3), we can reach indices 1 and 7. Since index 7 is the last index, we return True.

Step-by-Step Trace

class Solution:
    def solve(self, nums, k):
        n = len(nums)
        visited = [0] * n
        tovisit = [k]
        
        print(f"Starting at index {k}")
        
        while len(tovisit) > 0:
            i = tovisit.pop()
            print(f"Visiting index {i}, value = {nums[i]}")
            
            if i == n - 1:
                print(f"Reached end at index {i}!")
                return True
            
            if visited[i] != 1:
                visited[i] = 1
                up = i + nums[i]
                down = i - nums[i]
                
                if up < n:
                    tovisit.append(up)
                    print(f"  Can go right to index {up}")
                if down >= 0:
                    tovisit.append(down)
                    print(f"  Can go left to index {down}")
        
        return False

# Test with trace
ob = Solution()
nums = [0, 0, 2, 1, 3, 3, 1, 1]
k = 2
result = ob.solve(nums, k)
print(f"Final result: {result}")
Starting at index 2
Visiting index 2, value = 2
  Can go right to index 4
  Can go left to index 0
Visiting index 0, value = 0
Visiting index 4, value = 3
  Can go right to index 7
  Can go left to index 1
Visiting index 1, value = 0
Visiting index 7, value = 1
Reached end at index 7!
Final result: True

Conclusion

This solution uses BFS to explore all reachable positions from the starting index. The key insight is using a visited array to prevent cycles and efficiently checking if we can reach the last index of the list.

Updated on: 2026-03-25T10:43:30+05:30

426 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements