Learn-dsa..in 30 days!



























CC-11 : Find number of paths from top left element of matrix to bottom right element.

Description:

Given an input matrix, find number of paths from the top-left element of matrix to bottom-right element of matrix.

Test cases and expected outputs:

Input Parameters Expected outputs
3x3 matrix Paths from first node to last node in 3x3 matrix :
6

Pseudocode:

getMatrixStartToEndPaths(startX, startY, endX, endY):

The co-ordinates of top-left and bottom-right elements are received as input parameters .
If startX==endX or startY==endY, return 1; (base case).
Set int variable downPaths as return value from recursive call to getMatrixStartToEndPaths(startX+1, startY, endX, endY).
Set int variable rightPaths as return value from recursive call to getMatrixStartToEndPaths(startX, startY+1, endX, endY)
Return downPaths + rightPaths.

Code:

public int getMatrixStartToEndPaths(int startX,int startY, int endX, int endY){
	if ((startX==endX)||(startY==endY)){return 1;}
	int downPaths=getMatrixStartToEndPaths(startX+1, startY, endX, endY);
	int rightPaths=getMatrixStartToEndPaths(startX, startY+1, endX, endY);
	return downPaths+rightPaths;
}

Click here to download and run code and test cases !