Alternating Work Days Codechef Solution|Problem Code:ALTER

Alternating Work Days Codechef Solution


Alice and Bob are two friends. Initially, the skill levels of them are zero. They work on alternative days, i.e one of Alice and Bob works on the odd-numbered days(1,3,5,…)(1,3,5,…) and the other works on the even-numbered days (2,4,6,…)(2,4,6,…). The skill levels of Alice and Bob increase by A,BA,B respectively on the days they work.

Determine if it is possible that the skill levels of Alice and Bob become exactly P,QP,Q respectively on some day.

Input Format

  • The first line contains an integer TT, denoting the number of test cases. The TT test cases then follow:
  • The first and only line of each test case contains four space-separated integers A,B,P,QA,B,P,Q.

Output Format

For each test case, print YES if it is possible that the skill levels of Alice and Bob become exactly P,QP,Q on some day, otherwise print NO.

You may print each character of the string in uppercase or lowercase (for example, the strings “yEs”, “yes”, “Yes” and “YES” will all be treated as identical).


  • 1≤T≤1031≤T≤103
  • 1≤A,B,P,Q≤1091≤A,B,P,Q≤109


  • Subtask 1 (100 points): Original constraints

Sample Input 1 

1 2 1 2
1 2 3 2
4 3 4 6
3 5 9 25

Sample Output 1 


Alternating Work Days – CodeChef Solution in JAVA

import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(;
    int T = sc.nextInt();
    for (int tc = 0; tc < T; ++tc) {
      int A = sc.nextInt();
      int B = sc.nextInt();
      int P = sc.nextInt();
      int Q = sc.nextInt();
      System.out.println(solve(A, B, P, Q) ? "YES" : "NO");
  static boolean solve(int A, int B, int P, int Q) {
    return P % A == 0 && Q % B == 0 && Math.abs(P / A - Q / B) <= 1;

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

Leave a Comment