Given an array of integers **Arr[]**. The elements of the array are sorted in increasing order. The task is to find the minimum number of elements to be inserted in the array so that the differences between any two consecutive elements are the same.**Examples:**

Input:Arr[] = {1, 4, 13, 19, 25}Output:4Explanation:

One possible solution is: Arr[] = { 1, 4, 7, 10, 13, 16, 19, 22, 25 }. Here all the consecutive elements has difference of 3, for this 4 elements are inserted.

Input:Arr[] = {1, 5, 8, 10, 12, 16};Output:10Explanation:

10 elements needs to be inserted in order to make the difference equal.

**Approach: **The idea is to calculate the **differences** between all consecutive elements. There are **N** elements in the array so there will be **N – 1** such differences.

- Find the GCD(greatest common divisor) of all such differences. This GCD will be the difference between any two consecutive elements of the array after insertion of new elements.
- Let’s suppose the difference between the first and second element is
**diff**. Initialize the answer by 0 and add**((diff / GCD) – 1)**to the answer because there are**( diff / GCD – 1 )**elements that are needed to make the differene equal to**GCD**. - Perform the same for all consecutive elements of the given array and add to the answer in order to find the minimum number of elements to be inserted to make equal differences between any two consecutive elements of the array.

Below is the implementation of the approach:

## C++

`// C++ program for the above approach ` ` ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to find gcd of two numbers ` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(b == 0) ` ` ` `return` `a; ` ` ` ` ` `return` `gcd(b, a % b); ` `} ` ` ` `// Function to calculate minimum ` `// numbers to be inserted to make ` `// equal differences between ` `// two consecutive elements ` `int` `minimum_elements(` `int` `n, ` `int` `arr[]) ` `{ ` ` ` `// Check if there is only one ` ` ` `// element in the array ` ` ` `// then answer will be 0 ` ` ` `if` `(n < 3) ` ` ` `return` `0; ` ` ` ` ` `int` `g, ans = 0, diff, cnt; ` ` ` ` ` `// Calculate difference ` ` ` `// between first and second ` ` ` `// element of array ` ` ` `diff = arr[1] - arr[0]; ` ` ` ` ` `// If there is only two elements ` ` ` `// in the array then gcd of ` ` ` `// differences of consecutive ` ` ` `// elements of array will be ` ` ` `// equal to difference of first ` ` ` `// and second element of the array ` ` ` `g = diff; ` ` ` ` ` `// Loop to calculate the gcd ` ` ` `// of the differences between ` ` ` `// consecutive elements of the array ` ` ` `for` `(` `int` `i = 2; i < n; i++) { ` ` ` `diff = arr[i] - arr[i - 1]; ` ` ` ` ` `g = gcd(g, diff); ` ` ` `} ` ` ` ` ` `// Loop to calculate the ` ` ` `// elements to be inserted ` ` ` `for` `(` `int` `i = 1; i < n; i++) { ` ` ` `diff = arr[i] - arr[i - 1]; ` ` ` ` ` `cnt = diff / g; ` ` ` ` ` `ans += (cnt - 1); ` ` ` `} ` ` ` ` ` `// Return the answer ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 5, 8, 10, 12, 16 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << minimum_elements(n, arr); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `import` `java.io.*; ` ` ` `class` `GFG{ ` ` ` `// Function to find gcd of two numbers ` `static` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(b == ` `0` `) ` ` ` `return` `a; ` ` ` ` ` `return` `gcd(b, a % b); ` `} ` ` ` `// Function to calculate minimum ` `// numbers to be inserted to make ` `// equal differences between ` `// two consecutive elements ` `static` `int` `minimum_elements(` `int` `n, ` `int` `arr[]) ` `{ ` ` ` ` ` `// Check if there is only one ` ` ` `// element in the array ` ` ` `// then answer will be 0 ` ` ` `if` `(n < ` `3` `) ` ` ` `return` `0` `; ` ` ` ` ` `int` `g, ans = ` `0` `, diff, cnt; ` ` ` ` ` `// Calculate difference ` ` ` `// between first and second ` ` ` `// element of array ` ` ` `diff = arr[` `1` `] - arr[` `0` `]; ` ` ` ` ` `// If there is only two elements ` ` ` `// in the array then gcd of ` ` ` `// differences of consecutive ` ` ` `// elements of array will be ` ` ` `// equal to difference of first ` ` ` `// and second element of the array ` ` ` `g = diff; ` ` ` ` ` `// Loop to calculate the gcd ` ` ` `// of the differences between ` ` ` `// consecutive elements of the array ` ` ` `for` `(` `int` `i = ` `2` `; i < n; i++) ` ` ` `{ ` ` ` `diff = arr[i] - arr[i - ` `1` `]; ` ` ` `g = gcd(g, diff); ` ` ` `} ` ` ` ` ` `// Loop to calculate the ` ` ` `// elements to be inserted ` ` ` `for` `(` `int` `i = ` `1` `; i < n; i++) ` ` ` `{ ` ` ` `diff = arr[i] - arr[i - ` `1` `]; ` ` ` `cnt = diff / g; ` ` ` `ans += (cnt - ` `1` `); ` ` ` `} ` ` ` ` ` `// Return the answer ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `arr[] = { ` `1` `, ` `5` `, ` `8` `, ` `10` `, ` `12` `, ` `16` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `System.out.println(minimum_elements(n, arr)); ` `} ` `} ` ` ` `// This code is contributed by sanjoy_62 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function to find gcd of two numbers ` `def` `gcd(a, b): ` ` ` ` ` `if` `(b ` `=` `=` `0` `): ` ` ` `return` `a ` ` ` ` ` `return` `gcd(b, a ` `%` `b) ` ` ` `# Function to calculate minimum ` `# numbers to be inserted to make ` `# equal differences between ` `# two consecutive elements ` `def` `minimum_elements(n, arr): ` ` ` ` ` `# Check if there is only one ` ` ` `# element in the array ` ` ` `# then answer will be 0 ` ` ` `if` `(n < ` `3` `): ` ` ` `return` `0` ` ` ` ` `ans ` `=` `0` ` ` ` ` `# Calculate difference ` ` ` `# between first and second ` ` ` `# element of array ` ` ` `diff ` `=` `arr[` `1` `] ` `-` `arr[` `0` `] ` ` ` ` ` `# If there is only two elements ` ` ` `# in the array then gcd of ` ` ` `# differences of consecutive ` ` ` `# elements of array will be ` ` ` `# equal to difference of first ` ` ` `# and second element of the array ` ` ` `g ` `=` `diff ` ` ` ` ` `# Loop to calculate the gcd ` ` ` `# of the differences between ` ` ` `# consecutive elements of the array ` ` ` `for` `i ` `in` `range` `(` `2` `, n): ` ` ` `diff ` `=` `arr[i] ` `-` `arr[i ` `-` `1` `] ` ` ` ` ` `g ` `=` `gcd(g, diff) ` ` ` ` ` `# Loop to calculate the ` ` ` `# elements to be inserted ` ` ` `for` `i ` `in` `range` `(` `1` `, n): ` ` ` `diff ` `=` `arr[i] ` `-` `arr[i ` `-` `1` `] ` ` ` `cnt ` `=` `diff ` `/` `/` `g ` ` ` `ans ` `+` `=` `(cnt ` `-` `1` `) ` ` ` ` ` `# Return the answer ` ` ` `return` `ans ` ` ` `# Driver code ` `arr ` `=` `[ ` `1` `, ` `5` `, ` `8` `, ` `10` `, ` `12` `, ` `16` `] ` `n ` `=` `len` `(arr) ` ` ` `print` `(minimum_elements(n, arr)) ` ` ` `# This code is contributed by sanjoy_62 ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find gcd of two numbers ` `static` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(b == 0) ` ` ` `return` `a; ` ` ` ` ` `return` `gcd(b, a % b); ` `} ` ` ` `// Function to calculate minimum ` `// numbers to be inserted to make ` `// equal differences between ` `// two consecutive elements ` `static` `int` `minimum_elements(` `int` `n, ` `int` `[] arr) ` `{ ` ` ` ` ` `// Check if there is only one ` ` ` `// element in the array ` ` ` `// then answer will be 0 ` ` ` `if` `(n < 3) ` ` ` `return` `0; ` ` ` ` ` `int` `g, ans = 0, diff, cnt; ` ` ` ` ` `// Calculate difference ` ` ` `// between first and second ` ` ` `// element of array ` ` ` `diff = arr[1] - arr[0]; ` ` ` ` ` `// If there is only two elements ` ` ` `// in the array then gcd of ` ` ` `// differences of consecutive ` ` ` `// elements of array will be ` ` ` `// equal to difference of first ` ` ` `// and second element of the array ` ` ` `g = diff; ` ` ` ` ` `// Loop to calculate the gcd ` ` ` `// of the differences between ` ` ` `// consecutive elements of the array ` ` ` `for` `(` `int` `i = 2; i < n; i++) ` ` ` `{ ` ` ` `diff = arr[i] - arr[i - 1]; ` ` ` `g = gcd(g, diff); ` ` ` `} ` ` ` ` ` `// Loop to calculate the ` ` ` `// elements to be inserted ` ` ` `for` `(` `int` `i = 1; i < n; i++) ` ` ` `{ ` ` ` `diff = arr[i] - arr[i - 1]; ` ` ` `cnt = diff / g; ` ` ` `ans += (cnt - 1); ` ` ` `} ` ` ` ` ` `// Return the answer ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` `int` `[] arr = { 1, 5, 8, 10, 12, 16 }; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `Console.WriteLine(minimum_elements(n, arr)); ` `} ` `} ` ` ` `// This code is contributed by sanjoy_62` |

*chevron_right*

*filter_none*

**Output:**

10

**Time Complexity:** O(N * 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.

## Recommended Posts:

- Count of elements to be inserted to make Array sum twice the XOR of Array
- Minimum elements inserted in a sorted array to form an Arithmetic progression
- Count of available non-overlapping intervals to be inserted to make interval [0, R]
- Smallest Integer to be inserted to have equal sums
- Make all array elements equal with minimum cost
- Minimum operation to make all elements equal in array
- Minimum number of increment-other operations to make all array elements equal.
- Minimum Bitwise AND operations to make any two array elements equal
- Minimum Increment / decrement to make array elements equal
- Minimum operations required to make all the array elements equal
- Find the minimum number of operations required to make all array elements equal
- Minimum value of X to make all array elements equal by either decreasing or increasing by X
- Minimum Bitwise OR operations to make any two array elements equal
- Minimum Bitwise XOR operations to make any two array elements equal
- Minimum cost to make all array elements equal
- Minimum decrement operations to make Array elements equal by only decreasing K each time
- Make array elements equal with minimum cost
- Minimum number of steps to make all elements of array equal
- Minimum operations to make all elements equal using the second array
- Minimum Cost to make all array elements equal using given operations

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.