Introduction
In this blog, we will try to grab hold of a two-dimensional array data structure problem. The problem aims to find the probability of an element of a matrix crossing the boundaries when moved by n positions.

Through this problem, we will try to understand how to approach any two-dimensional array data structure problem. We will learn about two-dimensional data structure and examine the problem statement. The examples presented will help you clear your concepts, the algorithm will provide you with an immediate solution, and the implementation part will give you a detailed understanding of every step.

Also Read About, Array Implementation of Queue and Rabin Karp Algorithm
What is Matrix?
A matrix can be made using a two-dimensional array data structure. A multidimensional array is an array within arrays. That is, each element of the multidimensional array is an array itself. Similarly, in a two-dimensional array, every element of an array is an array in itself.
For example, we use the following line of Java code to create a multidimensional array presented below.
int [][] a= new int [3][4];
This two-dimensional array can store up to 12 elements.

Two-Dimensional Array
Problem Statement
Firstly, we will have a look at what exactly the problem says.
We are given a matrix of a random order. We need to output the probability for any given vertex of a matrix that, when moved by n positions, never crosses any boundary.
The current cell can move in four directions, left, right, top, or bottom, with equal probability. Here, we have assumed that no index in the matrix remains empty and all the indices have some value. There are no exceptions to this problem.
To understand all the possible cases better, we have used the matrix of the same order in all the examples.
Sample Examples
🍇 Example 1: When a vertex is invalid.
Input: i = 4, j = 4 (Order of Matrix) x = 5, y = 3 (Coordinates of the Vertex) n = 3 (Shift in position) Output: 0.0 |
Explanation:
In the example above, you will observe a matrix of order 4*4. The input x and y added as coordinates of the vertex are not present inside the matrix boundary. This makes the vertex invalid. If the vertex is invalid, that is, outside the matrix boundary, its probability of crossing any boundary automatically becomes zero. This output is irrespective of whatever we input as a shift in position.
🍇 Example 2: When a vertex is valid, but there is no shift in position.
Input: i = 4, j = 4 (Order of Matrix) x = 2, y = 3 (Coordinates of the Vertex) n = 0 (Shift in position) Output: 1.0 |
Explanation:
In the example above, you will observe a matrix of order 4*4. The input x and y added as coordinates of the vertex are present inside the matrix boundary. This makes the vertex valid. Now, as the shift in position is zero and we already know that the vertex is inside the matrix boundary, its probability of remaining inside the boundary automatically becomes one. This output completely depends on the input added as a shift in position.
🍇 Example 3: When a vertex is valid, and position shift is equal to 3.
Input: i = 4, j = 4 (Order of Matrix) x = 2, y = 3 (Coordinates of the Vertex) n = 3 (Shift in position) Output: 0.453125 |
Explanation:
In the example above, you will observe a matrix of order 4*4. The input x and y added as coordinates of the vertex are present inside the matrix boundary. This makes the vertex valid. Now, as you can see, the shift in position is three, so the output is calculated based on the algorithm, with no exceptions. This output depends on the input added as a shift in position and on the vertex.
🍇 Example 4: When the vertex is valid, and the matrix has been crossed completely.
Input: i = 4, j = 4 (Order of Matrix) x = 4, y = 4 (Coordinates of the Vertex) n = 2 (Shift in position) Output: 0.0 |
Explanation:
In the example above, you will observe a matrix of order 4*4. The input x and y added as coordinates of the vertex are present on the matrix boundary. This makes the vertex valid. But, as you can see, the vertex is on the verge of crossing the boundary, so any value greater than zero for a shift in position would result in the vertex crossing the boundary. Thus, the probability becomes zero. This output depends on the input added as a shift in position and on the vertex.