Open In App

Check whether the number exists in Arithmetic sequence or not

Last Updated : 03 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given three integers ‘A’ denotes the first term of an arithmetic sequence, ‘C’ denotes the common difference between an arithmetic sequence and an integer ‘B’, the task is to tell whether ‘B’ exists in the arithmetic sequence or not. Return 1 if B is present in the sequence. Otherwise, returns 0.

Examples:

Input: A = 1, B = 3, C = 2
Output: 1
Explanation: 3 is the second term of the sequence starting with 1 and having a common difference 2.

Input: A = 1, B = 2, C = 3
Output: 0
Explanation: 2 is not present in the sequence.

Approach: To solve the problem follow the below idea:

To solve this problem you can use the concept of Arithmetic Progression. That is nth term of an Arithmetic sequence starting with ‘a’ and common difference ‘d’ is a + (n-1)*d.

Observations:

  • The first term is A. If B is greater than A, then C must be positive.
  • If B is lesser than A, then C must be negative.
  • Suppose d = difference between A and B. If B wants to exist in the arithmetic sequence, d must be divisible by C.

Below are the steps for the above approach:

  • Initialize a variable say, d that will store the difference between B and A.
    • d = B – A
  • Check if B is equal to A, then the term b is valid,
    • if(d == 0), return 1.
  • Check if d < 0, then the common difference must be negative and d must be divisible by the common difference, if both are not the case, return 0.
    • if(C ≥ 0), return 0.
    • if(d%C == 0), return 1.
  • If d is greater than or equal to 0, then the common difference must be greater than 0 and d must be divisible by the common difference, if both is not the case, return 0.
    • if(C ≤ 0), return 0.
    • if(d%C == 0), return 1.
  • Else, return 0.

Below is the code for the above approach:

C++




// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
 
int inSequence(int A, int B, int C)
{
    int d = (B - A);
    if (d == 0)
        return 1;
    if (d < 0) {
        if (C >= 0)
            return 0;
        if (d % C == 0)
            return 1;
        return 0;
    }
    else {
        if (C <= 0)
            return 0;
        if (d % C == 0)
            return 1;
        return 0;
    }
    return 0;
}
 
// Drivers code
int main()
{
 
    int a = 1, b = 3, c = 2;
    int ans = inSequence(a, b, c);
 
    // Function Call
    cout << ans;
    return 0;
}


Java




// Java code for the above approach:
import java.util.*;
 
class GFG {
    public static int inSequence(int A, int B, int C)
    {
        int d = (B - A);
        if (d == 0)
            return 1;
        if (d < 0) {
            if (C >= 0)
                return 0;
            if (d % C == 0)
                return 1;
            return 0;
        }
        else {
            if (C <= 0)
                return 0;
            if (d % C == 0)
                return 1;
        }
          return 0;
    }
 
    public static void main(String[] args)
    {
        int a = 1, b = 3, c = 2;
        int ans = inSequence(a, b, c);
 
        // Function Call
        System.out.println(ans);
    }
}
// This code is contributed by prasad264


Python3




#// Python code for the above approach
 
def inSequence(A: int, B: int, C: int) -> int:
    d = B - A
    if d == 0:
        return 1
    if d < 0:
        if C >= 0:
            return 0
        if d % C == 0:
            return 1
        return 0
    else:
        if C <= 0:
            return 0
        if d % C == 0:
            return 1
        return 0
 
# Driver code
if __name__ == '__main__':
    a, b, c = 1, 3, 2
    ans = inSequence(a, b, c)
 
    # Function Call
    print(ans)
 
# This code is contributed by Susobhan Akhuli


C#




// C# code for the above approach:
 
using System;
 
public class GFG {
      // Function to check whether the number
      // exists in Arithmetic sequence or not
    public static int inSequence(int A, int B, int C)
    {
        int d = (B - A);
        if (d == 0)
            return 1;
        if (d < 0)
        {
            if (C >= 0)
                return 0;
            if (d % C == 0)
                return 1;
            return 0;
        }
        else
        {
            if (C <= 0)
                return 0;
            if (d % C == 0)
                return 1;
            return 0;
        }
        return 0;
    }
 
    // Driver code
    public static void Main()
    {
        int a = 1, b = 3, c = 2;
        int ans = inSequence(a, b, c);
 
        // Function Call
        Console.WriteLine(ans);
    }
}


Javascript




function inSequence(A, B, C) {
  let d = B - A;
  if (d == 0) {
    return 1;
  }
  if (d < 0) {
    if (C >= 0) {
      return 0;
    }
    if (d % C == 0) {
      return 1;
    }
    return 0;
  } else {
    if (C <= 0) {
      return 0;
    }
    if (d % C == 0) {
      return 1;
    }
    return 0;
  }
}
 
// Drivers code
let a = 1,
  b = 3,
  c = 2;
let ans = inSequence(a, b, c);
 
// Function Call
console.log(ans)


Output

1

Time Complexity: O(1) // since no loop or recursion is used the algorithm takes up constant time to perform the operations
Auxiliary Space: O(1) // since no extra array or data structure is used so the space taken by the algorithm is constant
 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads