-
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.
- Loading branch information
1 parent
571ca16
commit d177c1a
Showing
1 changed file
with
53 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 |
---|---|---|
@@ -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)); | ||
} | ||
} | ||
} |