Minimum elements to be inserted in Array to make adjacent differences equal

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:
Explanation: 
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: 10 
Explanation: 
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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


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.




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.



Improved By : sanjoy_62

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.