Program to Find Minimum Jumps Required to Reach a Value with Different Parity in Python

Given a list of numbers, we can jump from index i to either i + nums[i] or i - nums[i] (if valid indices). We need to find the minimum jumps required from each position to reach a value with different parity (odd to even or even to odd).

If the input is numbers = [7, 3, 4, 5, 6, 9, 6, 7], the output will be [-1, 1, 2, -1, -1, -1, 1, -1].

Understanding the Problem

For each starting position, we use BFS to find the shortest path to any number with different parity. Two numbers have different parity if one is odd and the other is even.

Algorithm Steps

We follow these steps:

  • Define a BFS function to find minimum jumps from position i
  • Use a queue to track positions and distances
  • Check if current number has different parity than starting number
  • Explore valid jumps: current + nums[current] and current - nums[current]
  • Return the minimum distance or -1 if no path exists

Implementation

from collections import deque

class Solution:
    def solve(self, nums):
        def bfs(i):
            q = deque([(i, 0)])
            seen = set()
            
            while q:
                j, d = q.popleft()
                seen.add(j)
                
                # Check if current number has different parity than starting number
                if (nums[i] + nums[j]) % 2:
                    return d
                
                # Try both possible jumps
                for k in [j + nums[j], j - nums[j]]:
                    if 0 <= k < len(nums) and k not in seen:
                        q.append((k, d + 1))
            
            return 10 ** 10  # Return large number if no path found
        
        ans = []
        for i in range(len(nums)):
            x = bfs(i)
            ans.append(x if x < 10 ** 10 else -1)
        
        return ans

# Test the solution
ob = Solution()
numbers = [7, 3, 4, 5, 6, 9, 6, 7]
result = ob.solve(numbers)
print("Input:", numbers)
print("Output:", result)
Input: [7, 3, 4, 5, 6, 9, 6, 7]
Output: [-1, 1, 2, -1, -1, -1, 1, -1]

How It Works

For each starting position, the BFS explores all reachable positions level by level:

  • Position 0 (value 7): Cannot reach any even number ? -1
  • Position 1 (value 3): Can jump to position 4 (value 6) in 1 jump ? 1
  • Position 2 (value 4): Can reach odd number in 2 jumps ? 2
  • Position 6 (value 6): Can jump to position 0 (value 7) in 1 jump ? 1

Key Points

  • BFS guarantees finding the minimum number of jumps
  • Parity check: (nums[i] + nums[j]) % 2 is non-zero when numbers have different parity
  • The seen set prevents revisiting positions and infinite loops
  • Return -1 when no path to different parity exists

Conclusion

This solution uses BFS to find the minimum jumps required to reach a number with different parity from each starting position. The algorithm efficiently explores all possible paths while avoiding cycles using a visited set.

Updated on: 2026-03-25T14:03:45+05:30

281 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements