diff --git a/Paths to reach origin b/Paths to reach origin new file mode 100644 index 0000000..353506a --- /dev/null +++ b/Paths to reach origin @@ -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)); + } + } +}