Catch the Thief Codechef Solution

Catch the Thief Codechef Solution: A policeman wants to catch a thief. Both the policeman and the thief can only move on a line on integer coordinates between 00 and NN (inclusive).

Initially, the policeman is at a coordinate xx and the thief is at a coordinate yy. During each second, each of them must move to the left or right (not necessarily both in the same direction) by distance exactlyexactly equal to KK. No one may go to the left of the coordinate 00 or to the right of NN. Both the policeman and the thief move simultaneously and they cannot meet while moving, only at the end of each second.

Will the policeman be able to catch the thief if they both move optimally? The thief is caught as soon as the policeman and thief meet at the same position at the same time.

Input

  • The first line of the input contains a single integer TT denoting the number of test cases. The description of TT test cases follows.
  • The first and only line of each test case contains four space-separated integers xx, yy, KK and NN.

Output

For each test case, print a single line containing the string "Yes" if the thief can be caught or "No" if the thief cannot be caught (without quotes).

Constraints

  • 1≤T≤1,0001≤T≤1,000
  • 1≤N≤1091≤N≤109
  • 1≤K≤N1≤K≤N
  • 0≤x,y≤N0≤x,y≤N
  • x≠yx≠y

Sample Input 1 

5
0 1 1 1
1 4 1 5
4 2 1 7
3 7 2 10
8 2 3 15

Sample Output 1 

No
No
Yes
Yes
Yes

Explanation

Example case 1: The policeman is at 00 and the thief is at 11. After the 11-st second, the policeman is at 11 and the thief is at 00. After the next second, the policeman is again at 00 and the thief at 11. They cannot end up at the same coordinate

Catch the Thief   – CodeChef Solution in JAVA

import java.util.*;
public class Main {
	public static void main(String[] args) {
try{
   Scanner sc=new Scanner(System.in);
	int t=sc.nextInt();
	while(t-->0){
      long x=sc.nextLong();
      long y=sc.nextLong();
      long k=sc.nextLong();
      long n=sc.nextLong();
      if((Math.abs(x-y)%(2*k))==0)
       System.out.println("Yes");
      else
       System.out.println("No");
	}
	}catch(Exception e){return;}
	}
}

Catch the Thief   – CodeChef Solution in CPP

#include <iostream>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        int k = b - a;
        if (k < 0)
        {
            k = k * -1;
        }
        if ((k)%(2*c)==0)
        {
            cout << "Yes\n";
        }
        else
        {
            cout << "No\n";
        }
    }
    return 0;
}

Catch the Thief -CodeChef Solution in Python

t=int(input())
for _t in range(t):
    n,m,o,p=map(int,input().split(" "))
    if(abs(n-m)%(2*o)==0):
        print("Yes")
    else:
        print("No")
    

Disclaimer: The above Problem (Catch the Thief ) is generated by CodeChef but the solution is provided by  Chase2learn.This tutorial is only for Educational and Learning purpose.

Leave a Comment