Given four positive numbers **A, B, C, D**, such that A and B are the first term and the common difference of first Arithmetic sequence respectively, while C and D represent the same for the second Arithmetic sequence respectively as shown below:

First Arithmetic Sequence:

A, A + B, A + 2B, A + 3B, …….

Second Arithmetic Sequence:C, C + D, C + 2D, C + 3D, …….

The task is to find the least common element from the above AP sequences. If no such number exists, then print **-1**.

**Examples:**

Input:A = 2, B = 20, C = 19, D = 9Output:82Explanation:

Sequence 1: {2, 2 + 20, 2 + 2(20), 2 + 3(20), 2 + 4(20), …..} = {2, 22, 42, 62,82, …..}

Sequence 2: {19, 19 + 9, 19 + 2(9), 19 + 3(9), 19 + 4(9), 19 + 5(9), 19 + 6(9), 19 + 7(9) …..} = {19, 28, 37, 46, 55, 64, 73,82, …..}

Therefore, 82 is the smallest common element.

Input:A = 2, B = 18, C = 19, D = 9Output:-1

**Approach:**

Since any term of the given two sequences can be expressed as **A + x*B** and **C + y*D**, therefore, to solve the problem, we need to find the smallest values of x and y for which the two terms are equal.

Follow the steps below to solve the problem:

- In order to find the smallest value common in both the AP sequences, the idea is to find the smallest integer values of x and y satisfying the following equation:

A + x*B = C + y*D

=> The above equation can be rearranged as

x*B = C – A + y*D

=> The above equation can be further rearranged as

x = (C – A + y*D) / B

- Check if there exists any integer value
**y**such that**(C – A + y*D) % B**is**0**or not. If it exists, then the smallest number is**(C + y*D)**. - Check whether
**(C + y*D)**is the answer or not, where**y**will be in a range**(0, B)**because from**i = B, B+1,**…. the remainder values will be repeated. - If no such number is obtained from the above steps, then print
**-1**.

Below is the implementation of the above approach:

## C++

`// C++ program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the smallest element` `// common in both the subsequences` `long` `smallestCommon(` `long` `a, ` `long` `b,` ` ` `long` `c, ` `long` `d)` `{` ` ` `// If a is equal to c` ` ` `if` `(a == c)` ` ` `return` `a;` ` ` `// If a exceeds c` ` ` `if` `(a > c) {` ` ` `swap(a, c);` ` ` `swap(b, d);` ` ` `}` ` ` `long` `first_term_diff = (c - a);` ` ` `long` `possible_y;` ` ` `// Check for the satisfying` ` ` `// equation` ` ` `for` `(possible_y = 0; possible_y < b; possible_y++) {` ` ` `// Least value of possible_y` ` ` `// satisfying the given equation` ` ` `// will yield true in the below if` ` ` `// and break the loop` ` ` `if` `((first_term_diff % b` ` ` `+ possible_y * d)` ` ` `% b` ` ` `== 0) {` ` ` `break` `;` ` ` `}` ` ` `}` ` ` `// If the value of possible_y` ` ` `// satisfying the given equation` ` ` `// lies in range [0, b]` ` ` `if` `(possible_y != b) {` ` ` `return` `c + possible_y * d;` ` ` `}` ` ` `// If no value of possible_y` ` ` `// satisfies the given equation` ` ` `return` `-1;` `}` `// Driver Code` `int` `main()` `{` ` ` `long` `A = 2, B = 20, C = 19, D = 9;` ` ` `cout << smallestCommon(A, B, C, D);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement` `// the above approach` `import` `java.util.*;` `class` `GFG{` `// Function to find the smallest element` `// common in both the subsequences` `static` `int` `smallestCommon(` `int` `a, ` `int` `b,` ` ` `int` `c, ` `int` `d)` `{` ` ` `// If a is equal to c` ` ` `if` `(a == c)` ` ` `return` `a;` ` ` `// If a exceeds c` ` ` `if` `(a > c) ` ` ` `{` ` ` `swap(a, c);` ` ` `swap(b, d);` ` ` `}` ` ` `int` `first_term_diff = (c - a);` ` ` `int` `possible_y;` ` ` `// Check for the satisfying` ` ` `// equation` ` ` `for` `(possible_y = ` `0` `; ` ` ` `possible_y < b; possible_y++) ` ` ` `{` ` ` `// Least value of possible_y` ` ` `// satisfying the given equation` ` ` `// will yield true in the below if` ` ` `// and break the loop` ` ` `if` `((first_term_diff % b + ` ` ` `possible_y * d) % b == ` `0` `) ` ` ` `{` ` ` `break` `;` ` ` `}` ` ` `}` ` ` `// If the value of possible_y` ` ` `// satisfying the given equation` ` ` `// lies in range [0, b]` ` ` `if` `(possible_y != b) ` ` ` `{` ` ` `return` `c + possible_y * d;` ` ` `}` ` ` `// If no value of possible_y` ` ` `// satisfies the given equation` ` ` `return` `-` `1` `;` `}` ` ` `static` `void` `swap(` `int` `x, ` `int` `y)` `{` ` ` `int` `temp = x;` ` ` `x = y;` ` ` `y = temp;` `}` ` ` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `A = ` `2` `, B = ` `20` `, C = ` `19` `, D = ` `9` `;` ` ` `System.out.print(smallestCommon(A, B, C, D));` `}` `}` `// This code is contributed by PrinciRaj1992` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement` `# the above approach` `# Function to find the smallest element` `# common in both the subsequences` `def` `smallestCommon(a, b, c, d):` ` ` ` ` `# If a is equal to c` ` ` `if` `(a ` `=` `=` `c):` ` ` `return` `a;` ` ` `# If a exceeds c` ` ` `if` `(a > c):` ` ` `swap(a, c);` ` ` `swap(b, d);` ` ` `first_term_diff ` `=` `(c ` `-` `a);` ` ` `possible_y ` `=` `0` `;` ` ` `# Check for the satisfying` ` ` `# equation` ` ` `for` `possible_y ` `in` `range` `(b):` ` ` `# Least value of possible_y` ` ` `# satisfying the given equation` ` ` `# will yield True in the below if` ` ` `# and break the loop` ` ` `if` `((first_term_diff ` `%` `b ` `+` ` ` `possible_y ` `*` `d) ` `%` `b ` `=` `=` `0` `):` ` ` `break` `;` ` ` `# If the value of possible_y` ` ` `# satisfying the given equation` ` ` `# lies in range [0, b]` ` ` `if` `(possible_y !` `=` `b):` ` ` `return` `c ` `+` `possible_y ` `*` `d;` ` ` `# If no value of possible_y` ` ` `# satisfies the given equation` ` ` `return` `-` `1` `;` `def` `swap(x, y):` ` ` `temp ` `=` `x;` ` ` `x ` `=` `y;` ` ` `y ` `=` `temp;` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `A ` `=` `2` `; B ` `=` `20` `; C ` `=` `19` `; D ` `=` `9` `;` ` ` `print` `(smallestCommon(A, B, C, D));` `# This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement` `// the above approach` `using` `System;` `class` `GFG{` ` ` `// Function to find the smallest element` `// common in both the subsequences` `static` `int` `smallestCommon(` `int` `a, ` `int` `b,` ` ` `int` `c, ` `int` `d)` `{` ` ` `// If a is equal to c` ` ` `if` `(a == c)` ` ` `return` `a;` ` ` `// If a exceeds c` ` ` `if` `(a > c) ` ` ` `{` ` ` `swap(a, c);` ` ` `swap(b, d);` ` ` `}` ` ` `int` `first_term_diff = (c - a);` ` ` `int` `possible_y;` ` ` `// Check for the satisfying` ` ` `// equation` ` ` `for` `(possible_y = 0; ` ` ` `possible_y < b; possible_y++) ` ` ` `{` ` ` `// Least value of possible_y` ` ` `// satisfying the given equation` ` ` `// will yield true in the below if` ` ` `// and break the loop` ` ` `if` `((first_term_diff % b + ` ` ` `possible_y * d) % b == 0) ` ` ` `{` ` ` `break` `;` ` ` `}` ` ` `}` ` ` `// If the value of possible_y` ` ` `// satisfying the given equation` ` ` `// lies in range [0, b]` ` ` `if` `(possible_y != b) ` ` ` `{` ` ` `return` `c + possible_y * d;` ` ` `}` ` ` `// If no value of possible_y` ` ` `// satisfies the given equation` ` ` `return` `-1;` `}` ` ` `static` `void` `swap(` `int` `x, ` `int` `y)` `{` ` ` `int` `temp = x;` ` ` `x = y;` ` ` `y = temp;` `}` ` ` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `int` `A = 2, B = 20, C = 19, D = 9;` ` ` `Console.Write(smallestCommon(A, B, C, D));` `}` `}` `// This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

**Output:**

82

**Time Complexity:** O(B)**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count the Arithmetic sequences in the Array of size at least 3
- Nearest element with at-least one common prime factor
- Count common elements in two arrays which are in Arithmetic Progression
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- Longest arithmetic progression with the given common difference
- Find an integer that is common in the maximum number of given arithmetic progressions
- Arithmetic Progression containing X and Y with least possible first term
- Least Common Denominator (LCD)
- Count divisors of n that have at-least one digit common with n
- Minimize length of Substrings containing at least one common Character
- Check if all the Nodes in a Binary Tree having common values are at least D distance apart
- Sum of minimum element of all sub-sequences of a sorted array
- Print all sequences of given length
- Count of sub-sequences which satisfy the given condition
- Find if it is possible to make a binary string which contanins given number of "0", "1" , "01" and "10" as sub sequences
- Print all non-increasing sequences of sum equal to a given number x
- Convert given array to Arithmetic Progression by adding an element
- Change one element in the given array to make it an Arithmetic Progression
- Divide the two given numbers by their common divisors
- Number of common tangents between two circles if their centers and radius is given

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.