You’re given three integers X, C, and Y, where X denotes the first term of an arithmetic sequence whose common difference is C. Your task is to determine whether Y belongs to this arithmetic sequence or not.
For example:If X is 6 and C is 3, the sequence becomes 6,9,12,15,18,21,24…
The first line of input contains T, the number of test cases.
The first line of each test case contains three space-separated integers X, Y, and C as described in the problem statement.
Output Format:
For each test case print in a new line “True”, if Y belongs to the sequence otherwise “False”.
Note:
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10^5
-10^8 <= X,C,Y <= 10^8
Time Limit: 1 sec
2
6 18 3
6 17 3
True
False
In the first case when X is 6 and C is 3, the sequence becomes 6,9,12,15,18,21,24…, in which 18 is a part of this sequence and 17 is not. Hence we print True for the first case and False for the second.
2
4 2 -1
8 4 -3
True
False
This is a mathematics question, hence we need to observe a few things and then make cases accordingly to reach our answer.
If the common difference is 0 and both X and Y are equal, this implies that Y is the part of the sequence so we print “True”.
If you observe carefully, you’ll see that if we encounter the above case our answer will always be “False” because if the first term(X) is greater than Y and C is positive then there’s no chance that Y can occur in this sequence because if X > Y, then for Y to be a part of this sequence, C has to be negative and since C > 0, we’ll always get “False”. Similarly if X < Y, then if C is positive then only Y will be a part of the arithmetic sequence, but here C < 0, we’ll get “False”.
In this case, we are checking that if the difference between X and Y is a multiple of C or not because if that condition is met, then Y will surely exist in the sequence and our answer will be “True”.
For example : X = 3, C = 3, and Y = 18
Now, (18 - 3 )% 3 = 15 % 3 = 0, this means Y is a part of this arithmetic sequence, which is true because this arithmetic sequence will be 3,6,9,12,15,18,21…. Where 18 is the 6th term of this sequence.
O(1)
As we are just checking some inequalities.
O(1)
As we are using constant extra memory.