📌  相关文章
📜  从给定的操作中最大化数组中可能的不同元素的数量

📅  最后修改于: 2021-09-03 04:02:01             🧑  作者: Mango

给定大小为N的数组A[] ,任务是通过插入现有数组元素的绝对差异来最大化数组中不同元素的计数。

例子:

天真的方法:
从数组中生成所有可能的对并将它们的绝对差异存储在一个集合中。将所有数组元素插入到集合中。集合的最终大小表示执行给定操作后数组可以拥有的最大不同元素。
时间复杂度: O(N 2 )
辅助空间: O(N)
有效的方法:
按照以下步骤优化上述方法:

  1. 求数组的最大元素。任意两个元素的最大可能绝对差不超过数组的最大元素。
  2. 计算数组的 GCD,因为它是整个数组的公因子。
  3. 将最大元素除以数组的 gcd 以获得不同元素的计数。

下面是上述方法的实现:

C++
// C++ program to find the maximum
// possible distinct elements that
// can be obtained from the array
// by performing the given operations
#include 
using namespace std;
 
// Function to find the gcd
// of two numbers
int gcd(int x, int y)
{
    if (x == 0)
        return y;
    return gcd(y % x, x);
}
 
// Function to calculate and return
// the count of maximum possible
// distinct elements in the array
int findDistinct(int arr[], int n)
{
    // Find the maximum element
    int maximum = *max_element(arr,
                               arr + n);
 
    // Base Case
    if (n == 1)
        return 1;
 
    if (n == 2) {
        return (maximum / gcd(arr[0],
                              arr[1]));
    }
 
    // Finding the gcd of first
    // two element
    int k = gcd(arr[0], arr[1]);
 
    // Calculate Gcd of the array
    for (int i = 2; i < n; i++) {
        k = gcd(k, arr[i]);
    }
 
    // Return the total count
    // of distinct elements
    return (maximum / k);
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << findDistinct(arr, n);
    return 0;
}


Java
// Java program to find the maximum
// possible distinct elements that
// can be obtained from the array
// by performing the given operations
import java.util.*;
 
class GFG{
     
// Function to find the gcd
// of two numbers
static int gcd(int x, int y)
{
    if (x == 0)
        return y;
    return gcd(y % x, x);
}
 
// Function to calculate and return
// the count of maximum possible
// distinct elements in the array
static int findDistinct(int arr[], int n)
{
     
    // Find the maximum element
    int maximum = Arrays.stream(arr).max().getAsInt();
 
    // Base Case
    if (n == 1)
        return 1;
 
    if (n == 2)
    {
        return (maximum / gcd(arr[0],
                              arr[1]));
    }
 
    // Finding the gcd of first
    // two element
    int k = gcd(arr[0], arr[1]);
 
    // Calculate Gcd of the array
    for(int i = 2; i < n; i++)
    {
        k = gcd(k, arr[i]);
    }
 
    // Return the total count
    // of distinct elements
    return (maximum / k);
}
 
// Driver code
public static void main (String[] args)
{
    int arr[] = { 1, 2, 3, 5 };
    int n = arr.length;
 
    System.out.println(findDistinct(arr, n));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to find the maximum
# possible distinct elements that
# can be obtained from the array
# by performing the given operations
 
# Function to find the gcd
# of two numbers
def gcd(x, y):
     
    if (x == 0):
        return y
    return gcd(y % x, x)
 
# Function to calculate and return
# the count of maximum possible
# distinct elements in the array
def findDistinct(arr, n):
     
    # Find the maximum element
    maximum = max(arr)
     
    # Base Case
    if (n == 1):
        return 1
 
    if (n == 2):
        return (maximum // gcd(arr[0],
                               arr[1]))
 
    # Finding the gcd of first
    # two element
    k = gcd(arr[0], arr[1])
 
    # Calculate Gcd of the array
    for i in range(2, n):
        k = gcd(k, arr[i])
 
    # Return the total count
    # of distinct elements
    return (maximum // k)
 
# Driver Code
if __name__ == '__main__':
 
    arr = [ 1, 2, 3, 5 ]
    n = len(arr)
 
    print(findDistinct(arr, n))
 
# This code is contributed by mohit kumar 29


C#
// C# program to find the maximum
// possible distinct elements that
// can be obtained from the array
// by performing the given operations
using System;
using System.Linq;
class GFG{
 
// Function to find the gcd
// of two numbers
static int gcd(int x, int y)
{
    if (x == 0)
        return y;
    return gcd(y % x, x);
}
 
// Function to calculate and return
// the count of maximum possible
// distinct elements in the array
static int findDistinct(int []arr, int n)
{
    // Find the maximum element
    int maximum = arr.Max();
 
    // Base Case
    if (n == 1)
        return 1;
 
    if (n == 2)
    {
        return (maximum / gcd(arr[0],
                              arr[1]));
    }
 
    // Finding the gcd of first
    // two element
    int k = gcd(arr[0], arr[1]);
 
    // Calculate Gcd of the array
    for (int i = 2; i < n; i++)
    {
        k = gcd(k, arr[i]);
    }
 
    // Return the total count
    // of distinct elements
    return (maximum / k);
}
 
// Driver Code
public static void Main()
{
    int []arr = { 1, 2, 3, 5 };
    int n = arr.Length;
 
    Console.Write(findDistinct(arr, n));
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
5

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

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