Dsa
Dynamic Programming
Easy
Balanced Paths
Given a 2D grid of integers where each cell can have a value of either 1 or -1, find the number of unique paths from the top-left corner (0, 0) to the bottom-right corner (n-1, m-1) such that the sum of values along the path is zero. You can only move right or down. If there are no valid paths, return 0.
Input format:
The first line contains two integers n and m (1 <= n, m <= 100), representing the dimensions of the grid.
The next n lines each contain m integers (1 or -1).
Output format:
Print a single integer, the number of unique paths that satisfy the condition.
Example:
Input:
3 3
1 1 -1
1 -1 1
-1 1 1
Output:
2
Key concepts
dynamic_programminggrid_traversal
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now