📌  相关文章
📜  插入到数组中以使相邻差相等的最小元素

📅  最后修改于: 2021-04-23 07:30:29             🧑  作者: Mango

给定一个整数数组Arr [] 。数组的元素按升序排序。任务是找到要在数组中插入的元素的最小数量,以使任意两个连续元素之间的差异相同。
例子:

方法:想法是计算所有连续元素之间的差异。数组中有N个元素,因此会有N – 1个这样的差异。

  • 找到所有这样的差异的GCD(最大公约数)。 GCD将是插入新元素后数组中任何两个连续元素之间的差。
  • 让我们假设第一个元素和第二个元素之间的区别是diff 。用0初始化答案,并在答案中加上((diff / GCD)– 1) ,因为要使差异等于GCD ,需要使用(diff / GCD – 1)元素。
  • 对给定数组的所有连续元素执行相同的操作,然后添加到答案中,以找到要插入的最小元素数,以使数组的任何两个连续元素之间具有相等的差。

下面是该方法的实现:

C++
// C++ program for the above approach
  
#include 
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


输出:
10

时间复杂度: O(N * log N)
辅助空间: O(1)