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

📅  最后修改于: 2021-05-08 19:07:00             🧑  作者: 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
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”