forked from AdarshAddee/Hacktoberfest2022_for_Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request AdarshAddee#51 from ShubhanshuJha/main
Count Number Of Paths To Reach From 1 To N Problem
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,12 @@ | |
| Sarthak Roy | <a href="https://github.com/sarthakroy2002">Sarthak Roy</a> | <a href="mailto:[email protected]">E-Mail</a> | | ||
| Hardik Pratap Singh | <a href="https://github.com/hardik-pratap-singh">Hardik Pratap Singh</a> | <a href="mailto:[email protected]">E-Mail</a> | | ||
| Suraj Bhandarkar S | <a href="https://github.com/Suraj-Bhandarkar-S">Adarsh Addee</a> | <a href="mailto:[email protected]">E-Mail</a> | | ||
| Shubhanshu Jha | <a href="https://github.com/ShubhanshuJha">ShubhanshuJha</a> | <a href="mailto:[email protected]">E-Mail</a> | | ||
| Dhruv Bhagatkar| <a href="https://github.com/dhruv2003">Dhruv Bhagatkar</a> | <a href="mailto:[email protected]">E-Mail</a> | | ||
|
||
|
||
|
||
|
||
<br> | ||
<h1> | ||
Happy Hacking ❤💻 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Google Online Coding Challenge: Counting paths | ||
* You are given a matrix A having N rows and M columns. The rows are numbered 1 to N from top to bottom and columns are | ||
* numbered 1 to M from left to right. You are allowed to move right and down only, i.e., if you are at cell (i,j), then | ||
* you can move to (i+1, j) and (i, j+1). You are not allowed to move outside the matrix. | ||
* Your task is to find the number of good paths starting from (1, 1) and ending at (N, M). | ||
* Good Path: If the sum of all elements that lie in the path is divisible by K, then the path is considered as good. | ||
* | ||
* @author: ShubhanshuJha | ||
* | ||
**/ | ||
|
||
import java.io.*; | ||
import java.util.Set; | ||
import java.util.HashSet; | ||
import java.util.Scanner; | ||
|
||
public class CountingTheNumberOfPathsToReachFrom1ToN { | ||
public static void main(String[] args) throws IOException{ | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
Scanner sc = new Scanner(System.in); | ||
|
||
int testCase = Integer.parseInt(br.readLine()); | ||
while(testCase-- > 0){ | ||
long n = Long.parseLong(br.readLine()); | ||
long m = Long.parseLong(br.readLine()); | ||
long a[][] = new long[(int)n][(int)m]; | ||
for (int i = 0; i < (int)n; i++) { | ||
for (int j = 0; j < (int)m; j++) { | ||
a[i][j] = sc.nextLong(); | ||
} | ||
} | ||
long k = Long.parseLong(br.readLine()); | ||
printMat(a); | ||
long res = paths(a, n, m); | ||
System.out.println(res); | ||
} | ||
} | ||
static void printMat(long[][] m){ | ||
for (int i = 0; i < m.length; i++) { | ||
for (int j = 0; j < m[0].length; j++) { | ||
System.out.print(m[i][j]+" "); | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
private static long paths(long a[][], long N, long M){ | ||
if (a[(int)N][(int)M] == 1 || a[(int)N][(int)M] == 1) { | ||
return 1; | ||
} | ||
return paths(a, N, M-1) + paths(a, M, N-1); | ||
} | ||
} |