Skip to content

Commit

Permalink
Create Paths to reach origin
Browse files Browse the repository at this point in the history
  • Loading branch information
dishathakurata authored Apr 24, 2024
1 parent 571ca16 commit d177c1a
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Paths to reach origin
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//Paths to reach origin

import java.io.*;
import java.util.*;

class Solution {
static int mod = 1000000007;

static int help(int x, int y, int dp[][]) {
if(x == 0 && y == 0) {
return 1;
}

if(x < 0 || y < 0) {
return 0;
}

if(dp[x][y] != -1) {
return dp[x][y];
}

int a = help(x - 1, y, dp);
int b = help(x, y - 1, dp);

return dp[x][y] = (a + b) % mod;
}

public static int ways(int n, int m) {
int dp[][] = new int[n + 1][m + 1];

for(int temp[] : dp) {
Arrays.fill(temp, -1);
}

return help(n, m, dp);
}
}

class Array {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int testcases = Integer.parseInt(br.readLine());

while(testcases-- > 0){
String line = br.readLine();
String[] elements = line.trim().split("\\s+");
int x = Integer.parseInt(elements[0]);
int y = Integer.parseInt(elements[1]);
Solution ob = new Solution();
System.out.println(ob.ways(x,y));
}
}
}

0 comments on commit d177c1a

Please sign in to comment.