📌  相关文章
📜  可以添加到集合中的最大差异元素

📅  最后修改于: 2021-04-21 22:22:55             🧑  作者: Mango

给定一个包含N个元素的集合,仅当它可以表示为| XY |时,才允许向该集合添加元素Z> 0。其中X和Y已经存在于集合中。添加元素Z后,可以将其用作集合中的元素以添加新元素。找出可以通过这种方式添加的最大元素数。

例子

Input : set = {2, 3}
Output : 1
The only element that can be added is 1.

Input : set = {4, 6, 10}
Output : 2
The 2 elements that can be added are 
(6-4) = 2 and (10-2) = 8.

此问题基于以下观察:

  • 可以插入的最大元素必须小于集合中当前的最大元素,因为2个整数的差不能超过任何整数。
  • 您插入的每个数字都必须是给定数组的gcd的倍数。由于在任何步骤,X和Y都是gcd的倍数,因此(XY)也是gcd的倍数。
  • 您可以插入小于最大元素的gcd的所有倍数。
  • 小于或等于可由gcd整除的max的项数是floor(max / gcd),这是执行所有插入后集合中元素的总数,我们需要删除原始N个元素的数量才能得到回答。

下面是上述方法的实现:

C++
// CPP program to find the maximum number
// of elements that can be added to a set
// such that it is the absolute difference
// of 2 elements already in the set
  
#include 
using namespace std;
  
// Function to find the maximum number
// of elements that can be added to a set
// such that it is the absolute difference
// of 2 elements already in the set
int maxNewElements(int a[], int n)
{
    int gcd = a[0];
  
    int mx = a[0];
  
    for (int i = 1; i < n; i++) {
        gcd = __gcd(gcd, a[i]);
        mx = max(mx, a[i]);
    }
  
    int total_terms = mx / gcd;
  
    return total_terms - n;
}
  
// Driver Code
int main()
{
    int a[] = { 4, 6, 10 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << maxNewElements(a, n);
    return 0;
}


Java
// Java program to find the maximum number
// of elements that can be added to a set
// such that it is the absolute difference
// of 2 elements already in the set
   
import java.util.*;
import java.lang.*;
import java.io.*;
  
  
class GFG{
      
static int __gcd(int a, int b) {
   if (b==0) return a;
   return __gcd(b,a%b);
}
// Function to find the maximum number
// of elements that can be added to a set
// such that it is the absolute difference
// of 2 elements already in the set
static int maxNewElements(int []a, int n)
{
    int gcd = a[0];
   
    int mx = a[0];
   
    for (int i = 1; i < n; i++) {
        gcd = __gcd(gcd, a[i]);
        mx = Math.max(mx, a[i]);
    }
   
    int total_terms = mx / gcd;
   
    return total_terms - n;
}
   
// Driver Code
public static void main(String args[])
{
    int a[] = { 4, 6, 10 };
    int n = a.length;
    System.out.print(maxNewElements(a, n));
}
}


Python 3
# Python3 program to find the maximum number 
# of elements that can be added to a set 
# such that it is the absolute difference 
# of 2 elements already in the set 
  
# from math lib import gcd method
from math import gcd
  
  
# Function to find the maximum number 
# of elements that can be added to a set 
# such that it is the absolute difference 
# of 2 elements already in the set 
def maxNewElements(a, n) :
  
    __gcd = a[0]
  
    mx = a[0]
  
    for i in range(1, n) :
        __gcd = gcd(__gcd,a[i])
        mx = max(mx, a[i])
  
    total_terms = mx / __gcd
  
    return total_terms - n
  
  
  
  
# Driver code
if __name__ == "__main__" :
  
    a = [ 4, 6, 10 ]
  
    n = len(a)
  
    print(int(maxNewElements(a,n)))
  
# This code is contributed by 
# ANKITRAI1


C#
// C# program to find the maximum 
// number of elements that can be 
// added to a set such that it is 
// the absolute difference of 2 
// elements already in the set
class GFG
{
      
static int __gcd(int a, int b) 
{
    if (b == 0) return a;
    return __gcd(b, a % b);
}
  
// Function to find the maximum number
// of elements that can be added to a set
// such that it is the absolute difference
// of 2 elements already in the set
static int maxNewElements(int[] a, int n)
{
    int gcd = a[0];
  
    int mx = a[0];
  
    for (int i = 1; i < n; i++)
    {
        gcd = __gcd(gcd, a[i]);
        mx = System.Math.Max(mx, a[i]);
    }
  
    int total_terms = mx / gcd;
  
    return total_terms - n;
}
  
// Driver Code
static void Main()
{
    int[] a = { 4, 6, 10 };
    int n = a.Length;
    System.Console.WriteLine(maxNewElements(a, n));
}
}
  
// This code is contributed by mits


PHP


输出:
2

时间复杂度: O(N * LogN)