Given the three integers N, X, and Y the task is to find an N length arithmetic progression series with the least possible first term containing X and Y.
Input: N = 5, X = 10, Y = 15
Output: 5 10 15 20 25
The least possible first term of the AP is 5. Common difference of AP = 5
The given AP contains 10 and 15.
Input: N = 10, X = 5, Y = 15
Output: 1 3 5 7 9 11 13 15 17 19
Naive Approach: The simplest approach is to iterate for all the values of possible common differences from 1 to abs(X-Y) and check if there exists an N length AP with the first term greater than 0 and containing both X and Y.
Time Complexity: O(N * abs(X-Y))
Auxiliary Space: O(1)
Efficient Approach: The approach is based on the idea that to include both X and Y in the series, the common difference of the AP must be a factor of abs(X-Y). Below are the steps to solve the problem:
- Iterate from 1 to sqrt(abs(X-Y)) and consider only those common differences which are factors of abs(X-Y).
- For every possible common difference say diff which divides abs(X-Y), find the minimum first term greater than 0 using binary search algorithm.
- Store the minimum first term and the corresponding common difference to print the N length Arithmetic Progression.
Below is the implementation of the above approach:
5 10 15 20 25
Time complexity: O(sqrt(abs(X-Y)) * log(N))
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.
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Ratio of mth and nth term in an Arithmetic Progression (AP)
- Program for N-th term of Arithmetic Progression series
- Minimize Nth term of an Arithmetic progression (AP)
- First term from given Nth term of the equation F(N) = (2 * F(N - 1)) % 10^9 + 7
- Longest string in non-decreasing order of ASCII code and in arithmetic progression
- Program for N-th term of Geometric Progression series
- Longest Arithmetic Progression | DP-35
- Check whether Arithmetic Progression can be formed from the given array
- Count of AP (Arithmetic Progression) Subsequences in an array
- Minimum De-arrangements present in array of AP (Arithmetic Progression)
- Program to print Arithmetic Progression series
- PHP program to print an arithmetic progression series using inbuilt functions
- Longest arithmetic progression with the given common difference
- Convert given array to Arithmetic Progression by adding an element
- Arithmetic Progression
- Change one element in the given array to make it an Arithmetic Progression
- Check whether nodes of Binary Tree form Arithmetic, Geometric or Harmonic Progression
- Minimum elements inserted in a sorted array to form an Arithmetic progression
- Count common elements in two arrays which are in Arithmetic Progression
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.