Given an array which is an original arithmetic progression with one element changed. The task is to make it an arithmetic progression again. If there are many such possible sequences, return any one of them. The length of the array will always be greater than 2.
Input : arr = [1, 3, 4, 7]
Output : arr = [1, 3, 5, 7]
The common difference for the arithmetic progression is 2, so the resulting
arithmetic progression is [1, 3, 5, 7].
Input : arr = [1, 3, 7]
Output : arr = [-1, 3, 7]
The common difference can be either 2 or 3 or 4.
Hence the resulting array can be either [1, 3, 5], [-1, 3, 7] or [1, 4, 7].
As any one can be chosen, [-1, 3, 7] is returned as the output.
The key components of an Arithmetic Progression are initial term and the common difference. So our task is to find these two values to know the actual Arithmetic Progression.
- If the length of the array is 3, take the common difference as the difference between any two elements.
- Otherwise, try to find the common difference using the first four elements.
- Check if the first three elements are in arithmetic progression using the formula a – a = a – a. If they are in arithmetic progression, the common difference d = a – a and the initial term would be a
- Check if the 2nd, 3rd and 4th elements are in arithmetic progression using the formula a – a = a – a. If they are in arithmetic progression, the common difference d = a – a, which means that the first element has been changed, so initial term is a = a – d
- In both of the above cases, we have checked if the first element or fourth element has been changed. If both the cases are false, then it means that first or fourth element was not changed. So the common difference can be taken as d = (a – a)/3 and the initial term = a
- Print all the elements using the initial term and common difference.
Below is the implementation of the above approach:
-1 3 7
Time Complexity: O(N), where N is the number of elements in the array.
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.
- Convert given array to Arithmetic Progression by adding an element
- Check whether Arithmetic Progression can be formed from the given array
- Longest arithmetic progression with the given common difference
- Count of AP (Arithmetic Progression) Subsequences in an array
- Minimum De-arrangements present in array of AP (Arithmetic Progression)
- Minimum elements inserted in a sorted array to form an Arithmetic progression
- Longest Arithmetic Progression | DP-35
- Program for N-th term of Arithmetic Progression series
- Program to print Arithmetic Progression series
- PHP program to print an arithmetic progression series using inbuilt functions
- Longest string in non-decreasing order of ASCII code and in arithmetic progression
- Ratio of mth and nth term in an Arithmetic Progression (AP)
- Arithmetic Progression
- Check whether nodes of Binary Tree form Arithmetic, Geometric or Harmonic Progression
- Count common elements in two arrays which are in Arithmetic Progression
- Find the missing number in unordered Arithmetic Progression
- Count of subarrays forming an Arithmetic Progression (AP)
- Longest subarray forming an Arithmetic Progression (AP)
- Arithmetic Progression containing X and Y with least possible first term
- Minimize Nth term of an Arithmetic progression (AP)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : Rajput-Ji