Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
[ad_1]
Given two integers X and Y, which signify the variety of rows and columns respectively of a matrix, the duty is to test whether or not there exists a path, Which follows all of the under circumstances:
Notice: Right here 1 based mostly indexing is used.
Examples:
Enter: X = 2, Y = 3
Output: YES
Rationalization:Every cell is traversed as soon as, beginning cell is (1, 1) and ending cell is (2, 1), Which is adjoining to (1, 1). Therefore, all of the circumstances met.
Enter: X = 1, Y = 1
Output: NO
Rationalization: As there isn’t any adjoining cell of ( 1, 1 ) exists due to this fact, Output is NO.
Strategy: The issue might be solved based mostly on the next statement:
Remark:
Let’s take some random test-cases.
Take a look at case 1: X = 4, Y = 4
Output: YES
Rationalization:Take a look at case 2: X=4, Y=5
Output: YES
Rationalization:Take a look at case 3: X = 1, Y =3
Output: NO
Rationalization: No path is feasible that fulfill given constraints.From all above check circumstances, We will conclude some circumstances:
- If each X and Y are odd, Then there exists no path.
- If (X = 1 && Y > 2) or (Y = 1 && X > 2), Then no path exists.
- All of the circumstances besides above mentioned 2( quantity 1 and quantity 2 ) circumstances can have a path.
Comply with the under steps to implement the thought:
Under is the implementation of the above strategy.
|
Time Complexity: O(1)
Auxiliary House: O(1)
[ad_2]