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

📅  最后修改于: 2021-05-04 16:25:22             🧑  作者: Mango

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

例子:

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

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

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

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

  • 遍历数组并计算给定数组X的GCD。
  • 找到数组中最大的元素,例如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.


输出:
3

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