📌  相关文章
📜  最小化由所有可能对之间的差异组成的数组的长度

📅  最后修改于: 2021-09-03 14:34:52             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是找到需要插入到数组中的最小元素数,使得数组中存在所有可能对的绝对差异。

例子:

朴素的方法:解决这个问题的最简单的方法是从数组中找到每个可能对的绝对差异,并检查获得的差异是否存在于数组中。如果不存在,则插入获得的差异。否则,打印插入数组的元素计数。

时间复杂度: O(N * X),其中 X 是数组中的最大元素。
辅助空间: O(X)

高效的方法:上述方法可以基于以下观察进行优化:

请按照以下步骤解决问题:

  • 遍历数组并计算给定数组的 GCD,例如X
  • 找到数组的最大元素,比如Max
  • 通过插入所有可能对的绝对差,数组中元素的总数是(Max) / X
  • 因此,插入数组的元素总数等于((Max / X) – N)

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the GCD of an array
int findGcd(int arr[], int N)
{
 
    // Stores GCD of an array
    int gcd = arr[0];
 
    // Traverse the array
    for (int i = 1; i < N; i++) {
 
        // Update gcd
        gcd = __gcd(gcd, arr[i]);
    }
    return gcd;
}
 
// Function to find minimum count of elements
// inserted into the array such that absolute
// difference of pairs present in the array
void findMax(int arr[], int N)
{
 
    // Stores gcd of the array
    int gcd = findGcd(arr, N);
 
    // Stores the largest elemetn
    // in the array
    int Max = INT_MIN;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Update Max
        Max = max(Max, arr[i]);
    }
 
    // Stores minimum count of elements inserted
    // into the array such that absolute difference
    // of pairs present in the array
    int ans = (Max / gcd) - N;
 
    cout << ans;
}
 
// Driver Code
int main()
{
 
    // Given array
    int arr[] = { 3, 5 };
 
    // Size of the array
    int N = (sizeof(arr) / (sizeof(arr[0])));
 
    // Function Call
    findMax(arr, N);
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
     
// Recursive function to return gcd of a and b
static int gcdd(int a, int b)
{
    // Everything divides 0
    if (a == 0)
       return b;
    if (b == 0)
       return a;
   
    // base case
    if (a == b)
        return a;
   
    // a is greater
    if (a > b)
        return gcdd(a - b, b);
    return gcdd(a, b - a);
}
       
// Function to find the GCD of an array
static int findGcd(int arr[], int N)
{
 
    // Stores GCD of an array
    int gcd = arr[0];
 
    // Traverse the array
    for (int i = 1; i < N; i++)
    {
 
        // Update gcd
        gcd = gcdd(gcd, arr[i]);
    }
    return gcd;
}
 
// Function to find minimum count of elements
// inserted into the array such that absolute
// difference of pairs present in the array
static void findMax(int arr[], int N)
{
 
    // Stores gcd of the array
    int gcd = findGcd(arr, N);
 
    // Stores the largest elemetn
    // in the array
    int Max = Integer.MIN_VALUE;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
        // Update Max
        Max = Math.max(Max, arr[i]);
    }
 
    // Stores minimum count of elements inserted
    // into the array such that absolute difference
    // of pairs present in the array
    int ans = (Max / gcd) - N;
    System.out.println(ans);
}
   
// Driver code
public static void main(String[] args)
{
   
    // Given array
    int arr[] = { 3, 5 };
 
    // Size of the array
    int N = arr.length;
 
    // Function Call
    findMax(arr, N);
}
}
 
// This code is contributed by susmitakundugoaldanga.


Python3
# Python3 program for the above approach
import math
import sys
 
# Function to find the GCD of an array
def findGcd(arr, N):
     
    # Stores GCD of an array
    gcd = arr[0]
     
    # Traverse the array
    for i in range(1, N):
         
        # Update gcd
        gcd = math.gcd(gcd, arr[i])
 
    return gcd
 
# Function to find minimum count of elements
# inserted into the array such that absolute
# difference of pairs present in the array
def findMax(arr, N):
     
    # Stores gcd of the array
    gcd = findGcd(arr, N)
     
    # Stores the largest elemetn
    # in the array
    Max = -sys.maxsize - 1
     
    # Traverse the array
    for i in range(N):
         
        # Update Max
        Max = max(Max, arr[i])
 
    # Stores minimum count of elements inserted
    # into the array such that absolute difference
    # of pairs present in the array
    ans = (Max // gcd) - N
 
    print(ans)
 
# Driver Code
if __name__ == "__main__":
     
    # Given array
    arr = [3, 5]
     
    # Size of the array
    N = len(arr)
 
    # Function Call
    findMax(arr, N)
 
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
 
class GFG
{
 
    // Recursive function to return gcd of a and b
    static int gcdd(int a, int b)
    {
       
        // Everything divides 0
        if (a == 0)
            return b;
        if (b == 0)
            return a;
 
        // base case
        if (a == b)
            return a;
 
        // a is greater
        if (a > b)
            return gcdd(a - b, b);
        return gcdd(a, b - a);
    }
 
    // Function to find the GCD of an array
    static int findGcd(int[] arr, int N)
    {
 
        // Stores GCD of an array
        int gcd = arr[0];
 
        // Traverse the array
        for (int i = 1; i < N; i++)
        {
 
            // Update gcd
            gcd = gcdd(gcd, arr[i]);
        }
        return gcd;
    }
 
    // Function to find minimum count of elements
    // inserted into the array such that absolute
    // difference of pairs present in the array
    static void findMax(int[] arr, int N)
    {
 
        // Stores gcd of the array
        int gcd = findGcd(arr, N);
 
        // Stores the largest elemetn
        // in the array
        int Max = Int32.MinValue;
 
        // Traverse the array
        for (int i = 0; i < N; i++)
        {
 
            // Update Max
            Max = Math.Max(Max, arr[i]);
        }
 
        // Stores minimum count of elements inserted
        // into the array such that absolute difference
        // of pairs present in the array
        int ans = (Max / gcd) - N;
        Console.WriteLine(ans);
    }
 
    // Driver code
    static public void Main()
    {
 
        // Given array
        int[] arr = new int[] { 3, 5 };
 
        // Size of the array
        int N = arr.Length;
 
        // Function Call
        findMax(arr, N);
    }
}
 
// This code is contributed by Dharanendra L V.


Javascript


输出:
3

时间复杂度: O(N * Log(X)),其中 X 是数组中最大的元素。
辅助空间: O(1)

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