Change one element in the given array to make it an Arithmetic Progression

30 May, 2022
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. Examples:
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.
Approach: 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[1] – a[0] = a[2] – a[1]. If they are in arithmetic progression, the common difference d = a[1] – a[0] and the initial term would be a[0]Check if the 2nd, 3rd and 4th elements are in arithmetic progression using the formula a[2] – a[1] = a[3] – a[2]. If they are in arithmetic progression, the common difference d = a[2] – a[1], which means that the first element has been changed, so initial term is a[0] = a[1] – dIn 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[3] – a[0])/3 and the initial term = a[0]
Check if the first three elements are in arithmetic progression using the formula a[1] – a[0] = a[2] – a[1]. If they are in arithmetic progression, the common difference d = a[1] – a[0] and the initial term would be a[0]
Check if the 2nd, 3rd and 4th elements are in arithmetic progression using the formula a[2] – a[1] = a[3] – a[2]. If they are in arithmetic progression, the common difference d = a[2] – a[1], which means that the first element has been changed, so initial term is a[0] = a[1] – 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[3] – a[0])/3 and the initial term = a[0]
Print all the elements using the initial term and common difference.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program to change one element of an array such// that the resulting array is in arithmetic progression.#include <bits/stdc++.h>using namespace std;   // Finds the initial term and common difference and// prints the resulting sequence.void makeAP(int arr[], int n){    int initial_term, common_difference;       if (n == 3) {        common_difference = arr[2] - arr[1];        initial_term = arr[1] - common_difference;    }       else if ((arr[1] - arr[0]) == arr[2] - arr[1]) {           // Check if the first three elements are in        // arithmetic progression        initial_term = arr[0];        common_difference = arr[1] - arr[0];    }    else if ((arr[2] - arr[1]) == (arr[3] - arr[2])) {           // Check if the first element is not        // in arithmetic progression        common_difference = arr[2] - arr[1];        initial_term = arr[1] - common_difference;    }    else {           // The first and fourth element are        // in arithmetic progression        common_difference = (arr[3] - arr[0]) / 3;        initial_term = arr[0];    }       // Print the arithmetic progression    for (int i = 0; i < n; i++)        cout << initial_term + (i * common_difference) << " ";    cout << endl;}   // Driver Programint main(){    int arr[] = { 1, 3, 7 };    int n = sizeof(arr) / sizeof(arr[0]);       makeAP(arr, n);       return 0;}
// Java program to change one element of an array such// that the resulting array is in arithmetic progression.import java.util.Arrays;   class AP {    static void makeAP(int arr[], int n)    {        int initial_term, common_difference;           // Finds the initial term and common difference and        // prints the resulting array.        if (n == 3) {            common_difference = arr[2] - arr[1];            initial_term = arr[1] - common_difference;        }        else if ((arr[1] - arr[0]) == arr[2] - arr[1]) {               // Check if the first three elements are in            // arithmetic progression            initial_term = arr[0];            common_difference = arr[1] - arr[0];        }        else if ((arr[2] - arr[1]) == (arr[3] - arr[2])) {            // Check if the first element is not            // in arithmetic progression            common_difference = arr[2] - arr[1];            initial_term = arr[1] - common_difference;        }        else {            // The first and fourth element are            // in arithmetic progression            common_difference = (arr[3] - arr[0]) / 3;            initial_term = arr[0];        }           // Print the arithmetic progression        for (int i = 0; i < n; i++)            System.out.print(initial_term +                            (i * common_difference) + " ");        System.out.println();    }       // Driver code    public static void main(String[] args)    {        int arr[] = { 1, 3, 7 };        int n = arr.length;        makeAP(arr, n);    }}
# Python program to change one element of an array such# that the resulting array is in arithmetic progression.def makeAP(arr, n):    initial_term, common_difference = 0, 0    if (n == 3):        common_difference = arr[2] - arr[1]        initial_term = arr[1] - common_difference    elif((arr[1] - arr[0]) == arr[2] - arr[1]):         # Check if the first three elements are in        # arithmetic progression        initial_term = arr[0]        common_difference = arr[1] - arr[0]             elif((arr[2] - arr[1]) == (arr[3] - arr[2])):         # Check if the first element is not        # in arithmetic progression        common_difference = arr[2] - arr[1]        initial_term = arr[1] - common_difference             else:        # The first and fourth element are        # in arithmetic progression        common_difference = (arr[3] - arr[0]) / 3        initial_term = arr[0]     # Print the arithmetic progression    for i in range(n):        print(int(initial_term+                 (i * common_difference)), end = " ")    print()   # Driver codearr = [1, 3, 7]n = len(arr)makeAP(arr, n)
// C# program to change one element of an array such// that the resulting array is in arithmetic progression.using System; public class AP{    static void makeAP(int []arr, int n)    {        int initial_term, common_difference;         // Finds the initial term and common difference and        // prints the resulting array.        if (n == 3)        {            common_difference = arr[2] - arr[1];            initial_term = arr[1] - common_difference;        }        else if ((arr[1] - arr[0]) == arr[2] - arr[1])        {             // Check if the first three elements are in            // arithmetic progression            initial_term = arr[0];            common_difference = arr[1] - arr[0];        }        else if ((arr[2] - arr[1]) == (arr[3] - arr[2]))        {            // Check if the first element is not            // in arithmetic progression            common_difference = arr[2] - arr[1];            initial_term = arr[1] - common_difference;        }        else        {            // The first and fourth element are            // in arithmetic progression            common_difference = (arr[3] - arr[0]) / 3;            initial_term = arr[0];        }         // Print the arithmetic progression        for (int i = 0; i < n; i++)            Console.Write(initial_term +                            (i * common_difference) + " ");        Console.WriteLine();    }     // Driver code    public static void Main(String[] args)    {        int []arr = { 1, 3, 7 };        int n = arr.Length;        makeAP(arr, n);    }} // This code contributed by Rajput-Ji
<script> // JavaScript program to change one element of an array such// that the resulting array is in arithmetic progression.function makeAP(arr, n){    let initial_term = 0, common_difference = 0    if (n == 3){        common_difference = arr[2] - arr[1]        initial_term = arr[1] - common_difference    }    else if((arr[1] - arr[0]) == arr[2] - arr[1]){         // Check if the first three elements are in        // arithmetic progression        initial_term = arr[0]        common_difference = arr[1] - arr[0]    }             else if((arr[2] - arr[1]) == (arr[3] - arr[2])){         // Check if the first element is not        // in arithmetic progression        common_difference = arr[2] - arr[1]        initial_term = arr[1] - common_difference    }             else{        // The first and fourth element are        // in arithmetic progression        common_difference = Math.floor((arr[3] - arr[0]) / 3)        initial_term = arr[0]    }     // Print the arithmetic progression    for(let i=0;i<n;i++){        document.write(initial_term+(i * common_difference)," ")    }    document.write("</br>")}   // Driver codelet arr = [1, 3, 7]let n = arr.lengthmakeAP(arr, n) // This code is contributed by shinjanpatra </script>
-1 3 7
Time Complexity: O(N), where N is the number of elements in the array.
Auxiliary Space: O(1)
Rajput-Ji
shinjanpatra
vansikasharma1329
arithmetic progression
Arrays
Mathematical
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Search, insert and delete in an unsorted array
Window Sliding Technique
Chocolate Distribution Problem
Find duplicates in O(n) time and O(1) extra space | Set 1
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Coin Change | DP-7