📌  相关文章
📜  插入排序数组以形成算术级数的最小元素

📅  最后修改于: 2021-09-05 11:36:49             🧑  作者: Mango

给定一个已排序的数组arr[] ,任务是找到需要插入数组的最小元素,以便数组形成算术级数。
例子:

做法:思路是先求排序数组中连续元素的差,然后求所有差的最大公约数。差异的 GCD 将是可以形成的等差数列的共同差异。下面是步骤的图示:

  • 找出数组的连续元素之间的差异并将其存储在diff[] 中
  • 现在diff[]数组的 GCD 将给出给定排序数组元素之间的共同差异。
    例如:
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}
  • 如果已排序数组arr[] 的连续元素之间的差异大于上面计算的 GCD,则需要在给定数组中插入的最小元素数使数组的元素在算术级数中由下式给出:
Number of possible elements = 
    (Difference / Common Difference) - 1
  • 添加需要为给定排序数组中的所有连续元素集插入的元素计数。

下面是上述方法的实现:

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[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
int main()
{
    int arr1[] = { 1, 6, 8, 10, 14, 16 };
    int n1 = sizeof(arr1)/sizeof(arr1[0]);
    // 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[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
    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


Javascript


输出:
10

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live