Alternating Work Days Codechef Solution

Hello Programmers In this post, you will know how to solve the Alternating Work Days Codechef Solution. The Problem Code: ALTER

Alternating Work Days Codechef Solution
Alternating Work Days Codechef Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.


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 BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Next: Alternating subarray prefix Codechef Solution

Leave a Reply

Your email address will not be published. Required fields are marked *