Professional Documents
Culture Documents
L32 Maneuvering
L32 Maneuvering
WHAT IS MANEUVERING?
The task is to count all the possible paths from top left to bottom right
of a m x n matrix with the constraints that from each cell you can either
move only to right or down.
Input: M = 2, N = 2
Output: 2
Explanation: There are two paths
(0, 0) -> (0, 1) -> (1, 1)
(0, 0) -> (1, 0) -> (1, 1)
ALGORITHM
• Declare a 2-D array count of size M * N
• Set value of count[i][0] equal to 1 for 0 <= i < M as the answer of
subproblem with a single column is equal to 1
• Set value of count[0][j] equal to 1 for 0 <= j < N as the answer of
subproblem with a single row is equal to 1
• Create a nested for loop for 0 <= i < M and 0 <= j < N and assign
count[i][j] equal to count[i-1][j] + count[i][j-1]
• Print value of count[M-1][N-1]
import java.io.*;
class GFG {
// Returns count of possible paths to reach
// cell at row number m and column number n from
// the topmost leftmost cell (cell at 1, 1)
static int numberOfPaths(int m, int n)
{
// Create a 2D table to store results
// of subproblems
int count[][] = new int[m][n];