-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion_1.java
More file actions
36 lines (32 loc) · 989 Bytes
/
Question_1.java
File metadata and controls
36 lines (32 loc) · 989 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class Question_1 {
private static int solve(int[][] grid, int m, int n, int i, int j, int xor){
if(i >= m || j >= n || i <0 || j<0){ //terminating condition
return 0;
}
xor = xor ^ grid[i][j]; //xor of the sum and the grid cell value
if(i == m-1 && j == n-1 && (xor) == 0){ //if at end of grid xor of path is 0 then return 1
return 1;
}
if(i == m-1 && j == n-1){ //if at end of grid and sum is not 0 then return 0
return 0;
}
return (solve(grid,m,n,i+1,j,xor) + solve(grid,m,n,i,j+1,xor)); //summing the total number of path obtained
}
public static void main(String[] args) {
int [][] grid= {{5,2,0},{2,0,2},{0,7,5}}; //input
int m=grid.length; //row
int n=grid[0].length; //col
int res=solve(grid,m,n,0,0,0); //function calling
System.out.println(res);
}
}
/*
input:
[[3,0,7],[7,0,3],[3,7,0]];
output:
2
input:
[[5,2,0],[2,0,2],[0,7,5]]
output:
3
*/