Check if it is possible to reach destination in even number of steps in an Infinite Matrix
Last Updated :
11 Aug, 2022
Given a source and destination in a matrix[][] of infinite rows and columns, the task is to find whether it is possible to reach the destination from the source in an even number of steps. Also, you can only move up, down, left, and right.
Examples:
Input: Source = {2, 1}, Destination = {1, 4}
Output: Yes
Input: Source = {2, 2}, Destination = {1, 4}
Output: No
Observation:
The observation is that if the steps required to reach the destination in the shortest path is even, then steps required in every other route to reach it will always be even. Also, there can be an infinite number of ways to reach the target point. Some paths to reach (4, 1) from (1, 2) in a 4 x 5 matrix are given below :
Minimum number of steps required =4
So our problem is reduced in finding the minimum number of steps required to reach the destination from source in a matrix, which can be calculated easily by simply taking the sum of the absolute values of the difference between the X coordinates and Y coordinates.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void IsEvenPath( int Source[], int Destination[])
{
int x_dif = abs (Source[0] - Destination[0]);
int y_dif = abs (Source[1] - Destination[1]);
int minsteps = x_dif + y_dif;
if (minsteps % 2 == 0)
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
int Source[] = { 2, 1 };
int Destination[] = { 1, 4 };
IsEvenPath(Source, Destination);
return 0;
}
|
Java
import java.lang.*;
import java.util.*;
class GFG{
static void IsEvenPath( int Source[], int Destination[])
{
int x_dif = Math.abs(Source[ 0 ] - Destination[ 0 ]);
int y_dif = Math.abs(Source[ 1 ] - Destination[ 1 ]);
int minsteps = x_dif + y_dif;
if (minsteps % 2 == 0 )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main(String[] args)
{
int Source[] = { 2 , 1 };
int Destination[] = { 1 , 4 };
IsEvenPath(Source, Destination);
}
}
|
Python3
def IsEvenPath(Source, Destination):
x_dif = abs (Source[ 0 ] - Destination[ 0 ])
y_dif = abs (Source[ 1 ] - Destination[ 1 ])
minsteps = x_dif + y_dif
if (minsteps % 2 = = 0 ):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
Source = [ 2 , 1 ]
Destination = [ 1 , 4 ]
IsEvenPath(Source, Destination)
|
C#
using System;
class GFG{
static void IsEvenPath( int [] Source, int [] Destination)
{
int x_dif = Math.Abs(Source[0] - Destination[0]);
int y_dif = Math.Abs(Source[1] - Destination[1]);
int minsteps = x_dif + y_dif;
if (minsteps % 2 == 0)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
public static void Main( string [] args)
{
int [] Source = { 2, 1 };
int [] Destination = { 1, 4 };
IsEvenPath(Source, Destination);
}
}
|
Javascript
<script>
function IsEvenPath(Source, Destination)
{
let x_dif = Math.abs(Source[0] - Destination[0]);
let y_dif = Math.abs(Source[1] - Destination[1]);
let minsteps = x_dif + y_dif;
if (minsteps % 2 == 0)
document.write( "Yes" );
else
document.write( "No" );
}
let Source = [2, 1];
let Destination = [1, 4];
IsEvenPath(Source, Destination);
</script>
|
Time Complexity: O(1) // since no loop is used the algorithm takes up constant time to perform the operations
Auxiliary Space: O(1) // since no extra array is used so the space taken by the algorithm is constant
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...