Given three integer a, b and c where 'a' denotes the first term of an arithmetic sequence, 'c' denotes the common difference of the arithmetic sequence, and an integer 'b', the task is to tell whether 'b' exists in the arithmetic sequence or not.
Examples:
Input: a = 1, b = 3, c = 2
Output: true
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: false
Explanation: 2 is not present in the sequence.
Try It Yourself
- The first term is a. If b is greater than a, then c must be positive.
- If b is less than a, then c must be negative.
- If b exists in the arithmetic sequence, (b-a) must be divisible by c.
// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
bool inSequence(int a, int b, int c)
{
int d = (b - a);
// d == 0 means b is part of sequence
if (d == 0)
return true;
// d is less than 0, c must be negative
// for b to be ahead of a
if (d < 0) {
if (c >= 0)
return false;
if (d % c == 0)
return true;
return false;
}
// d is greater than 0, c must be positive
// for b to be ahead of a
else {
if (c <= 0)
return false;
if (d % c == 0)
return true;
return false;
}
}
// Drivers code
int main()
{
int a = 1, b = 3, c = 2;
bool ans = inSequence(a, b, c);
if(ans == true){
cout << "True";
}else{
cout << "False";
}
return 0;
}
#include <stdio.h>
int inSequence(int a, int b, int c) {
int d = (b - a);
// d == 0 means b is part of sequence
if (d == 0)
return 1;
// d is less than 0, c must be negative
// for b to be ahead of a
if (d < 0) {
if (c >= 0)
return 0;
if (d % c == 0)
return 1;
return 0;
}
// d is greater than 0, c must be positive
// for b to be ahead of a
else {
if (c <= 0)
return 0;
if (d % c == 0)
return 1;
return 0;
}
}
int main() {
int a = 1, b = 3, c = 2;
int ans = inSequence(a, b, c);
if(ans == 1) {
printf("True\n");
} else {
printf("False\n");
}
return 0;
}
import java.util.*;
public class Main {
public static boolean inSequence(int a, int b, int c) {
int d = (b - a);
// d == 0 means b is part of sequence
if (d == 0)
return true;
// d is less than 0, c must be negative
// for b to be ahead of a
if (d < 0) {
if (c >= 0)
return false;
if (d % c == 0)
return true;
return false;
}
// d is greater than 0, c must be positive
// for b to be ahead of a
else {
if (c <= 0)
return false;
if (d % c == 0)
return true;
return false;
}
}
public static void main(String[] args) {
int a = 1, b = 3, c = 2;
boolean ans = inSequence(a, b, c);
if(ans == true){
System.out.println("True");
}else{
System.out.println("False");
}
}
}
def inSequence(a, b, c):
d = (b - a)
# d == 0 means b is part of sequence
if d == 0:
return True
# d is less than 0, c must be negative
# for b to be ahead of a
if d < 0:
if c >= 0:
return False
if d % c == 0:
return True
return False
# d is greater than 0, c must be positive
# for b to be ahead of a
else:
if c <= 0:
return False
if d % c == 0:
return True
return False
# Drivers code
a = 1
b = 3
c = 2
ans = inSequence(a, b, c)
if ans == True:
print('True')
else:
print('False')
using System;
public class Program {
public static bool inSequence(int a, int b, int c) {
int d = (b - a);
// d == 0 means b is part of sequence
if (d == 0)
return true;
// d is less than 0, c must be negative
// for b to be ahead of a
if (d < 0) {
if (c >= 0)
return false;
if (d % c == 0)
return true;
return false;
}
// d is greater than 0, c must be positive
// for b to be ahead of a
else {
if (c <= 0)
return false;
if (d % c == 0)
return true;
return false;
}
}
public static void Main() {
int a = 1, b = 3, c = 2;
bool ans = inSequence(a, b, c);
if(ans == true) {
Console.WriteLine("True");
} else {
Console.WriteLine("False");
}
}
}
function inSequence(a, b, c) {
let d = (b - a);
// d == 0 means b is part of sequence
if (d === 0)
return true;
// d is less than 0, c must be negative
// for b to be ahead of a
if (d < 0) {
if (c >= 0)
return false;
if (d % c === 0)
return true;
return false;
}
// d is greater than 0, c must be positive
// for b to be ahead of a
else {
if (c <= 0)
return false;
if (d % c === 0)
return true;
return false;
}
}
// Drivers code
let a = 1, b = 3, c = 2;
let ans = inSequence(a, b, c);
if(ans === true) {
console.log('True');
} else {
console.log('False');
}
Output
True
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