Open In App

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

Last Updated : 04 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 difference.

  • 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 difference 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;
}


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


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


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


Javascript




<script>
// Javascript program for the above approach
 
// Function to find gcd of two numbers
function 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
function minimum_elements(n, arr)
{
    // Check if there is only one
    // element in the array
    // then answer will be 0
    if (n < 3)
        return 0;
 
    let 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 (let i = 2; i < n; i++) {
        diff = arr[i] - arr[i - 1];
 
        g = gcd(g, diff);
    }
 
    // Loop to calculate the
    // elements to be inserted
    for (let i = 1; i < n; i++) {
        diff = arr[i] - arr[i - 1];
 
        cnt = parseInt(diff / g);
 
        ans += (cnt - 1);
    }
 
    // Return the answer
    return ans;
}
 
// Driver code
    let arr = [ 1, 5, 8, 10, 12, 16 ];
    let n = arr.length;
 
    document.write(minimum_elements(n, arr));
 
</script>


Output: 

10

Time Complexity: O(N * log N) 
Auxiliary Space: O(1), since no extra space has been taken.
 

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads