Convert given array to Arithmetic Progression by adding an element
Given an array arr[], the task is to find an element that can be added to the array in order to convert it to Arithmetic Progression. If it’s impossible to convert the given array into an AP then print -1.
Examples:
Input: arr[] = {3, 7}
Output: 11
3, 7 and 11 is a finite AP sequence.Input: a[] = {4, 6, 8, 15}
Output: -1
Approach:
- Sort the array and start traversing the array element by element and note the difference between the two consecutive elements.
- If the difference for all the elements is same then print last element + common difference.
- If the difference is different for at most one pair (arr[i – 1], arr[i]) and diff = 2 * common difference for all other elements then print arr[i] – common difference.
- Else print -1.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include<bits/stdc++.h> using namespace std; // Function to return the number to be // added int getNumToAdd(int arr[], int n) { sort(arr,arr+n); int d = arr[1] - arr[0]; int numToAdd = -1; bool numAdded = false; for (int i = 2; i < n; i++) { int diff = arr[i] - arr[i - 1]; // If difference of the current // consecutive elements is // different from the common // difference if (diff != d) { // If number has already been // chosen then it's not possible // to add another number if (numAdded) return -1; // If the current different is // twice the common difference // then a number can be added midway // from current and previous element if (diff == 2 * d) { numToAdd = arr[i] - d; // Number has been chosen numAdded = true; } // It's not possible to maintain // the common difference else return -1; } } // Return last element + common difference // if no element is chosen and the array // is already in AP if (numToAdd == -1) return (arr[n - 1] + d); // Else return the chosen number return numToAdd; } // Driver code int main() { int arr[] = { 1, 3, 5, 7, 11, 13, 15 }; int n = sizeof(arr)/sizeof(arr[0]); cout << getNumToAdd(arr, n); } // This code is contributed // by ihritik |
Java
// Java implementation of the approach import java.util.*; public class GFG { // Function to return the number to be // added static int getNumToAdd(int arr[], int n) { Arrays.sort(arr); int d = arr[1] - arr[0]; int numToAdd = -1; boolean numAdded = false; for (int i = 2; i < n; i++) { int diff = arr[i] - arr[i - 1]; // If difference of the current // consecutive elements is // different from the common // difference if (diff != d) { // If number has already been // chosen then it's not possible // to add another number if (numAdded) return -1; // If the current different is // twice the common difference // then a number can be added midway // from current and previous element if (diff == 2 * d) { numToAdd = arr[i] - d; // Number has been chosen numAdded = true; } // It's not possible to maintain // the common difference else return -1; } } // Return last element + common difference // if no element is chosen and the array // is already in AP if (numToAdd == -1) return (arr[n - 1] + d); // Else return the chosen number return numToAdd; } // Driver code public static void main(String[] args) { int arr[] = { 1, 3, 5, 7, 11, 13, 15 }; int n = arr.length; System.out.println(getNumToAdd(arr, n)); } } |
Python3
# Python 3 implementation of the approach # Function to return the number # to be added def getNumToAdd(arr, n): arr.sort(reverse = False) d = arr[1] - arr[0] numToAdd = -1 numAdded = False for i in range(2, n, 1): diff = arr[i] - arr[i - 1] # If difference of the current consecutive # elements is different from the common # difference if (diff != d): # If number has already been chosen # then it's not possible to add # another number if (numAdded): return -1 # If the current different is twice # the common difference then a # number can be added midway from # current and previous element if (diff == 2 * d): numToAdd = arr[i] - d # Number has been chosen numAdded = True # It's not possible to maintain # the common difference else: return -1 # Return last element + common difference # if no element is chosen and the array # is already in AP if (numToAdd == -1): return (arr[n - 1] + d) # Else return the chosen number return numToAdd # Driver code if __name__ == '__main__': arr = [1, 3, 5, 7, 11, 13, 15] n = len(arr) print(getNumToAdd(arr, n)) # This code is contributed # mohit kumar 29 |
C#
// C# implementation of the approach using System; public class GFG { // Function to return the number to be // added static int getNumToAdd(int []arr, int n) { Array.Sort(arr); int d = arr[1] - arr[0]; int numToAdd = -1; bool numAdded = false; for (int i = 2; i < n; i++) { int diff = arr[i] - arr[i - 1]; // If difference of the current // consecutive elements is // different from the common // difference if (diff != d) { // If number has already been // chosen then it's not possible // to add another number if (numAdded) return -1; // If the current different is // twice the common difference // then a number can be added midway // from current and previous element if (diff == 2 * d) { numToAdd = arr[i] - d; // Number has been chosen numAdded = true; } // It's not possible to maintain // the common difference else return -1; } } // Return last element + common difference // if no element is chosen and the array // is already in AP if (numToAdd == -1) return (arr[n - 1] + d); // Else return the chosen number return numToAdd; } // Driver code public static void Main() { int []arr = { 1, 3, 5, 7, 11, 13, 15 }; int n = arr.Length; Console.WriteLine(getNumToAdd(arr, n)); } } // This code is contributed // by ihritik |
PHP
9
Time Complexity : O(n Log n)
Recommended Posts:
- Change one element in the given array to make it an Arithmetic Progression
- 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)
- Adding elements of an array until every element becomes greater than or equal to k
- Arithmetic Progression
- Longest Arithmetic Progression | DP-35
- Ratio of mth and nth term in an Arithmetic Progression (AP)
- Program to print Arithmetic Progression series
- Program for N-th term of Arithmetic Progression series
- Longest arithmetic progression with the given common difference
- Find the missing number in Arithmetic Progression
- PHP program to print an arithmetic progression series using inbuilt functions
- Longest string in non-decreasing order of ASCII code and in arithmetic progression
- Number of positions such that adding K to the element is greater than sum of all other elements
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.



