# Minimum elements inserted in a sorted array to form an Arithmetic progression

Given a sorted array arr[], the task is to find minimum elements needed to be inserted in the array such that array forms an Arithmetic Progression.

Examples:

Input: arr[] = {1, 6, 8, 10, 14, 16}
Output: 10
Explanation:
Minimum elements required to form A.P. is 10.
Transformed array after insertion of the elements.
arr[] = {1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16}

Input: arr[] = {1, 3, 5, 7, 11}
Output: 1
Explanation:
Minimum elements required to form A.P. is 1.
Transformed array after insertion of the elements.
arr[] = {1, 3, 5, 7, 9, 11}

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to find the difference of consecutive elements of the sorted array and then find the greatest common divisor of all the differences. The GCD of the differences will be the common-difference for the arithmetic progression that can be formed. Below is the illustration of the steps:

• Find the difference between consecutive elements of the array and store in it diff[].
• Now the GCD of the diff[] array will give the common difference between the elements of given sorted array.
For Example:

```Given array be {1, 5, 7}
Difference of Consecutive elements will be -
Difference(1, 5) = |5 - 1| = 4
Difference(5, 7) = |7 - 5| = 2

Then, GCD of the Differences will be
gcd(4, 2) = 2

This means there can be A.P. formed
with common-difference as 2. That is -
{1, 3, 5, 7}
```
• If the difference between consecutive elements of the sorted array arr[] is greater than the GCD calculated above, then the minimum number of elements needed to be inserted in the given array to make the element of the array in Arithmetic Progression is given by:
```Number of possible elements =
(Difference / Common Difference) - 1
```
• Add the count of element needed to be insert for all set of consecutive elements in the given sorted array.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to find the ` `// minimum elements required to  ` `// be inserted into an array to ` `// form an arithmetic progression ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find the greatest  ` `// common divisor of two numbers ` `int` `gcdFunc(``int` `a, ``int` `b) ` `{ ` `    ``if` `(b == 0)  ` `        ``return` `a;  ` `    ``return` `gcdFunc(b, a % b); ` `} ` ` `  `// Function to find the minimum  ` `// the minimum number of elements ` `// required to be inserted into array ` `int` `findMinimumElements(``int``* a, ``int` `n) ` `{ ` `    ``int` `b[n - 1]; ` `     `  `    ``// Difference array of consecutive  ` `    ``// elements of the array ` `    ``for` `(``int` `i = 1; i < n; i++) { ` `        ``b[i - 1] = a[i] - a[i - 1]; ` `    ``} ` `    ``int` `gcd = b; ` `     `  `    ``// GCD of the difference array ` `    ``for` `(``int` `i = 0; i < n - 1; i++) { ` `        ``gcd = gcdFunc(gcd, b[i]); ` `    ``} ` `    ``int` `ans = 0; ` `     `  `    ``// Loop to calculate the minimum ` `    ``// number of elements required ` `    ``for` `(``int` `i = 0; i < n - 1; i++) { ` `        ``ans += (b[i] / gcd) - 1; ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `arr1[] = { 1, 6, 8, 10, 14, 16 }; ` `    ``int` `n1 = ``sizeof``(arr1)/``sizeof``(arr1); ` `    ``// Function calling ` `    ``cout << findMinimumElements(arr1, n1)  ` `         ``<< endl; ` `} `

## Java

 `// Java implementation to find the ` `// minimum elements required to  ` `// be inserted into an array to ` `// form an arithmetic progression ` `  `  `class` `GFG{ ` `  `  `    ``// Function to find the greatest  ` `    ``// common divisor of two numbers ` `    ``static` `int` `gcdFunc(``int` `a, ``int` `b) ` `    ``{ ` `        ``if` `(b == ``0``)  ` `            ``return` `a;  ` `        ``return` `gcdFunc(b, a % b); ` `    ``} ` `      `  `    ``// Function to find the minimum  ` `    ``// the minimum number of elements ` `    ``// required to be inserted into array ` `    ``static` `int` `findMinimumElements(``int``[] a, ``int` `n) ` `    ``{ ` `        ``int``[] b = ``new` `int``[n - ``1``]; ` `          `  `        ``// Difference array of consecutive  ` `        ``// elements of the array ` `        ``for` `(``int` `i = ``1``; i < n; i++) { ` `            ``b[i - ``1``] = a[i] - a[i - ``1``]; ` `        ``} ` `        ``int` `gcd = b[``0``]; ` `          `  `        ``// GCD of the difference array ` `        ``for` `(``int` `i = ``0``; i < n - ``1``; i++) { ` `            ``gcd = gcdFunc(gcd, b[i]); ` `        ``} ` `        ``int` `ans = ``0``; ` `          `  `        ``// Loop to calculate the minimum ` `        ``// number of elements required ` `        ``for` `(``int` `i = ``0``; i < n - ``1``; i++) { ` `            ``ans += (b[i] / gcd) - ``1``; ` `        ``} ` `        ``return` `ans; ` `    ``} ` `      `  ` ``// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `arr1[] = { ``1``, ``6``, ``8``, ``10``, ``14``, ``16` `}; ` `    ``int` `n1 = arr1.length; ` `    ``// Function calling ` `    ``System.out.print(findMinimumElements(arr1, n1)  ` `         ``+``"\n"``); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

## Python3

 `# Python3 implementation to find the ` `# minimum elements required to  ` `# be inserted into an array to ` `# form an arithmetic progression ` ` `  `# Function to find the greatest  ` `# common divisor of two numbers ` `def` `gcdFunc(a, b): ` `    ``if` `(b ``=``=` `0``): ` `        ``return` `a  ` `     `  `    ``return` `gcdFunc(b, a ``%` `b) ` ` `  `# Function to find the minimum  ` `# the minimum number of elements ` `# required to be inserted into array ` `def` `findMinimumElements(a, n): ` `    ``b ``=` `[``0``]``*``(n ``-` `1``) ` `     `  `    ``# Difference array of consecutive  ` `    ``# elements of the array ` `    ``for` `i ``in` `range``(``1``,n): ` `        ``b[i ``-` `1``] ``=` `a[i] ``-` `a[i ``-` `1``] ` `         `  `    ``gcd ``=` `b[``0``] ` ` `  `    ``# GCD of the difference array ` `    ``for` `i ``in` `range``(n``-``1``): ` `        ``gcd ``=` `gcdFunc(gcd, b[i]) ` `     `  `    ``ans ``=` `0` `     `  `    ``# Loop to calculate the minimum ` `    ``# number of elements required ` `    ``for` `i ``in` `range``(n``-``1``): ` `        ``ans ``+``=` `(b[i] ``/``/` `gcd) ``-` `1` `     `  `    ``return` `ans ` ` `  `# Driver Code ` `arr1 ``=` `[``1``, ``6``, ``8``, ``10``, ``14``, ``16``] ` `n1 ``=` `len``(arr1) ` `# Function calling ` `print``(findMinimumElements(arr1, n1)) ` ` `  `# This code is contributed by shubhamsingh10 `

## C#

 `// C# implementation to find the ` `// minimum elements required to  ` `// be inserted into an array to ` `// form an arithmetic progression ` `using` `System; ` ` `  `class` `GFG{ ` ` `  `    ``// Function to find the greatest  ` `    ``// common divisor of two numbers ` `    ``static` `int` `gcdFunc(``int` `a, ``int` `b) ` `    ``{ ` `        ``if` `(b == 0)  ` `            ``return` `a;  ` `        ``return` `gcdFunc(b, a % b); ` `    ``} ` `     `  `    ``// Function to find the minimum  ` `    ``// the minimum number of elements ` `    ``// required to be inserted into array ` `    ``static` `int` `findMinimumElements(``int``[] a, ``int` `n) ` `    ``{ ` `        ``int``[] b = ``new` `int``[n - 1]; ` `         `  `        ``// Difference array of consecutive  ` `        ``// elements of the array ` `        ``for` `(``int` `i = 1; i < n; i++) { ` `            ``b[i - 1] = a[i] - a[i - 1]; ` `        ``} ` `        ``int` `gcd = b; ` `         `  `        ``// GCD of the difference array ` `        ``for` `(``int` `i = 0; i < n - 1; i++) { ` `            ``gcd = gcdFunc(gcd, b[i]); ` `        ``} ` `        ``int` `ans = 0; ` `         `  `        ``// Loop to calculate the minimum ` `        ``// number of elements required ` `        ``for` `(``int` `i = 0; i < n - 1; i++) { ` `            ``ans += (b[i] / gcd) - 1; ` `        ``} ` `        ``return` `ans; ` `    ``} ` `     `  `    ``// Driver Code ` `    ``static` `public` `void` `Main () ` `    ``{ ` `        ``int``[] arr1 = ``new` `int``[] { 1, 6, 8, 10, 14, 16 }; ` `        ``int` `n1 = arr1.Length; ` `        ``// Function calling ` `        ``Console.WriteLine(findMinimumElements(arr1, n1)); ` `    ``} ` `} ` ` `  `// This code is contributed by shivanisingh `

Output:

```10
```

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.