Number In AP

Moderate
0/80
Average time to solve is 15m
profile
Contributed by
7 upvotes
Asked in companies
IntuitAdobeIon Trading

Problem statement

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…
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
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.
Constraints:
1 <= T <= 10^5
-10^8 <= X,C,Y <= 10^8

Time Limit: 1 sec
Sample Input 1:
2
6 18 3
6 17 3
Sample Output 1:
True
False
Explanation For Sample Input 1:
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.
Sample Input 2:
2
4 2 -1
8 4 -3
Sample Output 2:
True
False
Approaches (1)
Mathematics Based Approach

This is a mathematics question, hence we need to observe a few things and then make cases accordingly to reach our answer.

 

  • Case 1: If C = 0 and X = Y

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”. 

 

  • Case 2: If X > Y and C > 0, or X < Y and C <0

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”. 

 

  • Case 3: If (Y - X) % C = 0 and C is non zero

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. 

 

  • Case 4: When we don’t encounter any of the above three cases we simply print “False” since there isn't any case where it is possible for Y to be a part of this sequence.
Time Complexity

O(1)

 

As we are just checking some inequalities.

Space Complexity

O(1)

 

As we are using constant extra memory.

Code Solution
(100% EXP penalty)
Number In AP
Full screen
Console