Check if two coordinates can be made equal by incrementing/decrementing by K1 and K2 respectively
Last Updated :
24 Aug, 2021
Given two integer coordinates (X1, Y1) and (X2, Y2) and two positive integers K1 and K2, the task is to check if both the coordinates can be made equal by performing the following steps any number of times:
- Add or subtract K1 from either or both coordinates of (X1, Y1).
- Add or subtract K2 from either or both coordinates of (X2, Y2).
If it is possible to make (X1, Y1) and (X2, Y2) equal, then print Yes. Otherwise, print No.
Examples:
Input: X1 = 10, Y1 = 10, X2 = 18, Y2 = 13, K1 = 3, K2 = 4
Output: Yes
Explanation:
Following are the moves that can be taken to make both the coordinates equal:
- Move point (X1, Y1) as (10, 10) -> (10, 13).
- Move point (X2, Y2) as (18, 13) -> (14, 13) -> (10, 13)
From the above operations, both the coordinates can be made equal, then print Yes.
Input: X1 = 10, Y1 = 10, X2 = 18, Y2 = 13, K1 = 10, K2 = 10
Output: No
Approach: This problem can be solved using Greedy Approach based on the observation that the moves can be taken in x-direction for (X1, Y1) point is n1 and the moves taken in x-direction for (X2, Y2) point is n2, then the expression can be written as:
n1*K1 + n2*K2 = abs(X1 – X2),
…where n1 and n2 are non negative integers.
Similarly the same can be written for y-direction as:
n3*K1 + n4*K2 = abs(Y1 – Y2),
…where n3 and n4 are non negative integers.
Now, it can be seen that, the problem has been reduced to find if the above equation has solutions or not. If both equations have non-negative solutions then print ‘Yes’. Otherwise, print ‘No’.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int twoPointsReachable( int X1, int Y1, int X2, int Y2,
int K1, int K2)
{
int g = __gcd(K1, K2);
bool reachableOnX = 0;
int X_distance = abs (X1 - X2);
if (X_distance % g == 0) {
reachableOnX = 1;
}
bool reachableOnY = 0;
int Y_distance = abs (Y1 - Y2);
if (Y_distance % g == 0) {
reachableOnY = 1;
}
if (reachableOnY && reachableOnX) {
cout << "Yes"
<< "\n" ;
}
else {
cout << "No"
<< "\n" ;
}
return 0;
}
int main()
{
int X1 = 10, Y1 = 10, X2 = 18;
int Y2 = 13, K1 = 3, K2 = 4;
twoPointsReachable(X1, X2, Y1, Y2, K1, K2);
return 0;
}
|
Java
import java.util.*;
public class GFG{
static int __gcd( int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
static int twoPointsReachable( int X1, int Y1, int X2, int Y2,
int K1, int K2)
{
int g = __gcd(K1, K2);
boolean reachableOnX = (g == 0 );
int X_distance = Math.abs(X1 - X2);
if (X_distance % g == 0 ) {
reachableOnX = (g == 1 );
}
boolean reachableOnY = (g == 0 );
int Y_distance = Math.abs(Y1 - Y2);
if (Y_distance % g == 0 ) {
reachableOnY = (g == 1 );
}
if (reachableOnY && reachableOnX) {
System.out.print( "Yes" );
}
else {
System.out.print( "No" );
}
return 0 ;
}
public static void main (String[] args)
{
int X1 = 10 , Y1 = 10 , X2 = 18 ;
int Y2 = 13 , K1 = 3 , K2 = 4 ;
twoPointsReachable(X1, X2, Y1,
Y2, K1, K2);
}
}
|
Python3
from math import *
def twoPointsReachable(X1, Y1, X2, Y2, K1, K2):
g = gcd(K1, K2)
reachableOnX = 0
X_distance = abs (X1 - X2)
if (X_distance % g = = 0 ):
reachableOnX = 1
reachableOnY = 0
Y_distance = abs (Y1 - Y2)
if (Y_distance % g = = 0 ):
reachableOnY = 1
if (reachableOnY and reachableOnX):
print ( "Yes" )
else :
print ( "No" )
return 0
X1 = 10
Y1 = 10
X2 = 18
Y2 = 13
K1 = 3
K2 = 4
twoPointsReachable(X1, X2, Y1, Y2, K1, K2)
|
C#
using System;
public class GFG {
static int __gcd( int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
static int twoPointsReachable( int X1, int Y1, int X2,
int Y2, int K1, int K2)
{
int g = __gcd(K1, K2);
bool reachableOnX = Convert.ToBoolean(0);
int X_distance = Math.Abs(X1 - X2);
if (X_distance % g == 0) {
reachableOnX = Convert.ToBoolean(1);
}
bool reachableOnY = Convert.ToBoolean(0);
int Y_distance = Math.Abs(Y1 - Y2);
if (Y_distance % g == 0) {
reachableOnY = Convert.ToBoolean(1);
}
if (reachableOnY && reachableOnX) {
Console.Write( "Yes" );
}
else {
Console.Write( "No" );
}
return 0;
}
public static void Main(String[] args)
{
int X1 = 10, Y1 = 10, X2 = 18;
int Y2 = 13, K1 = 3, K2 = 4;
twoPointsReachable(X1, X2, Y1, Y2, K1, K2);
}
}
|
Javascript
<script>
function __gcd(a, b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
function twoPointsReachable(X1, Y1, X2, Y2,
K1, K2) {
let g = __gcd(K1, K2);
let reachableOnX = 0;
let X_distance = Math.abs(X1 - X2);
if (X_distance % g == 0) {
reachableOnX = 1;
}
let reachableOnY = 0;
let Y_distance = Math.abs(Y1 - Y2);
if (Y_distance % g == 0) {
reachableOnY = 1;
}
if (reachableOnY && reachableOnX) {
document.write( "Yes" + "<br>" );
}
else {
document.write( "No" + "<br>" );
}
return 0;
}
let X1 = 10, Y1 = 10, X2 = 18;
let Y2 = 13, K1 = 3, K2 = 4;
twoPointsReachable(X1, X2, Y1,
Y2, K1, K2);
</script>
|
Time Complexity: O(log(max(K1, K2)))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...