From d177c1a82ea2e1b075ec31b253f507dc61577186 Mon Sep 17 00:00:00 2001 From: Disha Thakurata <146114938+dishathakurata@users.noreply.github.com> Date: Wed, 24 Apr 2024 21:35:20 +0530 Subject: [PATCH] Create Paths to reach origin --- Paths to reach origin | 53 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Paths to reach origin 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)); + } + } +}