Arithmetic Number | Practice | GeeksforGeeks

Given three integers  'A' denoting the first term of an arithmetic sequence , 'C' denoting the common difference of an arithmetic sequence and an integer 'B'. you need to tell whether 'B' exists in the arithmetic sequence or not.
Example 1:
Input: A = 1, B = 3, C = 2
Output: 1
Explaination: 3 is the second term of the 
sequence starting with 1 and having a common 
difference 2.
Example 2:
Input: A = 1, B = 2, C = 3
Output: 0
Explaination: 2 is not present in the sequence.
Your Task:
You do not need to read input or print anything. Your task is to complete the function inSequence() which takes A, B and C and returns 1 if B is present in the sequence. Otherwise, returns 0.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
-109 ≤ A, B, C ≤ 109  
0
wajihafatima81025 days ago
int inSequence(int A, int B, int C){
     if(C==0){
         if(A==B)  return 1;
         return 0;
     }
    
    
    else if(C>0){
       
            if(B<A) return 0;
          
           if((B-A)%C==0)  return 1;
           return 0;
        }
     
   else {
   
        if(B>A) return 0;
        if((B-A)%C==0)  return 1;
        return 0;
    
       }
    return 0;
}
0
amarrajsmart1971 week ago
Time Taken=0.01 sec.
int inSequence(int A, int B, int C){       // code here       if(C==0)       {           if(A==B)           {               return 1;           }           else           {               return 0;           }       }        else if(B==A)       {           return 1;       }       else if((B-A)%C==0&&B>A&&C>0)       {           return 1;       }       else if((B-A)%C==0&&B<A&&C<0)       {           return 1;       }       else       {           return 0;       }   }
+1
kartikeyashokgautam3 weeks ago
Explained in Detail JAVA Solution : - 
 
static int inSequence(int A, int B, int C){                    if(C==0) // if common diff is 0 , then A and B must be same , therefore return 1;           return A==B?1:0; // But if Common diff = 0 , and A , B are not same , then it is not Arithmetic progression in that case will return 0                      if(B<A && C>0) return 0; //if common difference is positive , then B should be positive                      if(B>A && C<0) return 0; //if common difference is negative , then B should be negative                       int n = ((B-A)/C)+1; // nth number                      return (A+(n-1)*C==B?1:0);// using  Formula an = a + (n-1)d , here a = first term , n = nth number , d = common diff  , an = nth term          // if B is part of arithmetic  then it must be the nth term therefore return 1 else 0 
    }
0
kumarsurajroy36553 weeks ago
//User function Template for Java
class Solution{   static int inSequence(int A, int B, int C){       // code here              if(C==0){           return (A==B)?1:0;       }              int n = ((B-A)/C)+1;                     return (B==(A+((n-1)*C))&&n>0)?1:0;                 }}
+1
manyasmriti4 weeks ago
class Solution{
    static int inSequence(int a, int b, int c){
        // code here
        if(a == b) return 1;
        if(c == 0) return 0;
        if( (b - a) % c == 0 && b > a && c >0)
        return 1;
        else if( (b - a) % c == 0 && b < a && c <0)
        return 1;
        return 0;
    }
}
0
meetulsingh281084 weeks ago
HANDLE EVERY CASE WITH CARE || C++
    int inSequence(int A, int B, int C){       if(A==B){           return 1;       }       if(C==0){           return 0;       }       if((B-A)%C==0 && (B-A)/C>0){           return 1;       }       else{           return 0;       }   }
0
hoodninja4 weeks ago
    int inSequence(int A, int B, int C){
        // code here
        if(A==B)
            return 1;
        if(C == 0)
            return (A==B);


        int n;
        n = ((B-A)/C);
        
        if(n <= 0)
            return 0;
        
        if(B == A + (n*C))
        {
            return 1;
        }
        return 0;
    }
0
mashhadihossain1 month ago
SIMPLE JAVA SOLUTION (0.1/3.5 SEC)
class Solution{   static int inSequence(int A, int B, int C){       if(A==B)       {           return 1;       }       if(C==0)       {           if(A==B)           {               return 1;           }           else           {               return 0;           }       }       if(((B-A)%C)==0 && ((B-A)/C)>0)       {           return 1;       }       else       {           return 0;       }   }}
0
manish14091 month ago
c++ code with use of maths a.p formula
int inSequence(int A, int B, int C){       // code here       if(C == 0){          return(A==B);       }       // first corner case:if b smaller and c is graeter than 1        //then no chance of present        //eg 4 -12 1       //second corner case: eg -3 6 -2      if((B < A && C > 0)||(B > A && C < 0 )) return 0;      double c=C,b=B,a=A;      double temp =b-a;      float n=temp/c;      int check=n;      if(n-check==0)          return 1;      else        return 0;   }
0
mayank20212 months ago
This seems a simple question at first glance, but it gets v tricky when try to consider edge cases.At least, I couldn't submit it correctly in first few attempts , forgetting one case or other in every attempt.. good question..
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.