Check whether the number exists in Arithmetic sequence or not
Last Updated :
03 May, 2023
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.
- Check if B is equal to A, then the term b is valid,
- 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++
#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;
}
int main()
{
int a = 1, b = 3, c = 2;
int ans = inSequence(a, b, c);
cout << ans;
return 0;
}
|
Java
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);
System.out.println(ans);
}
}
|
Python3
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
if __name__ = = '__main__' :
a, b, c = 1 , 3 , 2
ans = inSequence(a, b, c)
print (ans)
|
C#
using System;
public 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;
}
return 0;
}
public static void Main()
{
int a = 1, b = 3, c = 2;
int ans = inSequence(a, b, c);
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;
}
}
let a = 1,
b = 3,
c = 2;
let ans = inSequence(a, b, c);
console.log(ans)
|
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
Share your thoughts in the comments
Please Login to comment...