Skip to content

Commit

Permalink
Merge pull request AdarshAddee#51 from ShubhanshuJha/main
Browse files Browse the repository at this point in the history
Count Number Of Paths To Reach From 1 To N Problem
  • Loading branch information
AdarshAddee authored Oct 1, 2022
2 parents b179ffd + 5befe66 commit 495c2f4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ❤💻
Expand Down
53 changes: 53 additions & 0 deletions Java/CountingTheNumberOfPathsToReachFrom1ToN.java
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);
}
}

0 comments on commit 495c2f4

Please sign in to comment.