Check if adjacent cell of Start can be reached visiting all cells once
Given two integers X and Y, which represent the number of rows and columns respectively of a matrix, the task is to check whether there exists a path, Which follows all the below conditions:
- Starting of the path is from the cell (1, 1).
- Ending of the path is any cell adjacent to (1, 1).
- You have to traverse each cell of the matrix only once.
Note: Here 1 based indexing is used.
Examples:
Input: X = 2, Y = 3
Output: YES
Explanation:
Explanation for input test case
Each cell is traversed once, starting cell is (1, 1) and ending cell is (2, 1), Which is adjacent to (1, 1). Hence, all the conditions met.
Input: X = 1, Y = 1
Output: NO
Explanation: As there is no adjacent cell of ( 1, 1 ) exists therefore, Output is NO.
Approach: The problem can be solved based on the following observation:
Observation:
Let’s take some random test-cases.
Test case 1: X = 4, Y = 4
Output: YES
Explanation:
Explanation for test case 1
Test case 2: X=4, Y=5
Output: YES
Explanation:
Explanation for test case 2
Test case 3: X = 1, Y =3
Output: NO
Explanation: No path is possible that satisfy given constraints.
From all above test cases, We can conclude some conditions:
- If both 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 the cases except above discussed 2( number 1 and number 2 ) cases will have a path.
Follow the below steps to implement the idea:
- Check the conditions on X and Y discussed above.
- If the values match any of the first two conditions then no path is possible.
- Otherwise, there exists a path.
Below is the implementation of the above approach.
C++
#include <iostream>
using namespace std;
string Is_Path_Possible( long X, long Y)
{
if (X % 2 != 0 && Y % 2 != 0 || X == 1 && Y > 2
|| Y == 1 && X > 2) {
return "NO" ;
}
else {
return "YES" ;
}
}
int main()
{
long X = 2, Y = 2;
cout << (Is_Path_Possible(X, Y));
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static void main(String args[])
{
long X = 2 , Y = 2 ;
System.out.println(Is_Path_Possible(X, Y));
}
static String Is_Path_Possible( long X, long Y)
{
if (X % 2 != 0 && Y % 2 != 0 || X == 1 && Y > 2
|| Y == 1 && X > 2 ) {
return "NO" ;
}
else {
return "YES" ;
}
}
}
|
Python3
def Is_Path_Possible(X, Y):
if (X % 2 ! = 0 and Y % 2 ! = 0 or X = = 1 and Y > 2 or Y = = 1 and X > 2 ):
return "NO"
else :
return "YES"
X, Y = 2 , 2
print (Is_Path_Possible(X, Y))
|
C#
using System;
public class GFG {
public static void Main( string []args)
{
long X = 2, Y = 2;
Console.WriteLine(Is_Path_Possible(X, Y));
}
static String Is_Path_Possible( long X, long Y)
{
if (X % 2 != 0 && Y % 2 != 0 || X == 1 && Y > 2
|| Y == 1 && X > 2) {
return "NO" ;
}
else {
return "YES" ;
}
}
}
|
Javascript
const Is_Path_Possible = (X, Y) => {
if (X % 2 != 0 && Y % 2 != 0 || X == 1 && Y > 2
|| Y == 1 && X > 2) {
return "NO" ;
}
else {
return "YES" ;
}
}
let X = 2, Y = 2;
console.log(Is_Path_Possible(X, Y));
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
23 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...