📌  相关文章
📜  在所有可能的对之间插入绝对差后,计算非重复数组元素

📅  最后修改于: 2021-05-17 20:07:42             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是通过在给定数组的所有可能对之间重复插入绝对差来最大化不同数组元素的数量。

例子:

天真的方法:解决此问题的最简单方法是从给定数组中重复选择一个对,并插入该对的绝对差。最后,检查数组中是否已经存在所有可能的对的绝对差。如果发现为真,则将不同元素的数量打印到数组中。

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

高效方法:这个想法是利用以下事实:可以通过从较大的数中反复减去较小的数,直到两个元素相等,来获得两个数的GCD。因此,将元素插入数组,使所有相邻元素之间的绝对差必须等于数组的GCD。请按照以下步骤解决问题:

  • 找到数组中最大的元素,例如Max
  • 找到数组的GCD,例如GCDArr
  • 最后,打印((Max / GCDArr)+1)的值

下面是上述方法的实现:

C++
// C++ program of the above approach
#include 
using namespace std;
 
// Function to find the gcd of
// the two numbers
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
// Function to find distinct elements in the array
// by repeatidely inserting the absolute difference
// of all possible pairs
int DistinctValues(int arr[], int N)
{
 
    // Stores largest element
    // of the array
    int max_value = INT_MIN;
 
    // Traverse the array, arr[]
    for (int i = 0; i < N; ++i) {
 
        // Update max_value
        max_value = max(max_value, arr[i]);
    }
 
    // Stores GCD of array
    int GCDArr = arr[0];
 
    // Traverse the array, arr[]
    for (int i = 1; i < N; ++i) {
 
        // Update GCDArr
        GCDArr = gcd(GCDArr, arr[i]);
    }
 
    // Stores distinct elements in the array by
    // repeatidely inserting absolute difference
    // of all possible pairs
    int answer = (max_value / GCDArr) + 1;
 
    return answer;
}
 
// Driver Code
int main()
{
 
    // Given array arr[]
    int arr[] = { 4, 12, 16, 24 };
 
    int N = sizeof(arr) / sizeof(int);
 
    cout << DistinctValues(arr, N);
 
    return 0;
}
// This code is contributed by hemanth gadarla


Java
// Java program of the above approach
import java.util.*;
class GFG
{
   
// Function to find the gcd of
// the two numbers
static int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
// Function to find distinct elements in the array
// by repeatidely inserting the absolute difference
// of all possible pairs
static int DistinctValues(int arr[], int N)
{
 
    // Stores largest element
    // of the array
    int max_value = Integer.MIN_VALUE;
 
    // Traverse the array, arr[]
    for (int i = 0; i < N; ++i)
    {
 
        // Update max_value
        max_value = Math.max(max_value, arr[i]);
    }
 
    // Stores GCD of array
    int GCDArr = arr[0];
 
    // Traverse the array, arr[]
    for (int i = 1; i < N; ++i)
    {
 
        // Update GCDArr
        GCDArr = gcd(GCDArr, arr[i]);
    }
 
    // Stores distinct elements in the array by
    // repeatidely inserting absolute difference
    // of all possible pairs
    int answer = (max_value / GCDArr) + 1;
    return answer;
}
    
// Driver code
public static void main(String[] args)
{
   
    // Given array arr[]
    int arr[] = { 4, 12, 16, 24 };
    int N = arr.length;
    System.out.println(DistinctValues(arr, N));
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program of the above approach
import sys
  
# Function to find the gcd of
# the two numbers
def gcd(a, b):
     
    if a == 0:
        return b
         
    return gcd(b % a, a)
 
# Function to find distinct elements in
# the array by repeatidely inserting the
# absolute difference of all possible pairs
def DistinctValues(arr, N):
     
    # Stores largest element
    # of the array
    max_value = -sys.maxsize - 1
     
    # Update max_value
    max_value = max(arr)
 
    # Stores GCD of array
    GCDArr = arr[0]
 
    # Traverse the array, arr[]
    for i in range(1, N):
         
        # Update GCDArr
        GCDArr = gcd(GCDArr, arr[i])
 
     # Stores distinct elements in the array by
     # repeatedely inserting absolute difference
     # of all possible pairs
    answer = max_value // GCDArr
 
    return answer + 1
 
# Driver code
 
# Given array arr[]
arr = [ 4, 12, 16, 24 ]
N = len(arr)
 
print(DistinctValues(arr, N))
 
# This code is contributed by hemanth gadarla


C#
// C# program of the above approach
using System;
class GFG
{
 
  // Function to find the gcd of
  // the two numbers
  static int gcd(int a, int b)
  {
    if (a == 0)
      return b;
    return gcd(b % a, a);
  }
 
  // Function to find distinct elements in the array
  // by repeatidely inserting the absolute difference
  // of all possible pairs
  static int DistinctValues(int[] arr, int N)
  {
 
    // Stores largest element
    // of the array
    int max_value = Int32.MinValue;
 
    // Traverse the array, arr[]
    for (int i = 0; i < N; ++i)
    {
 
      // Update max_value
      max_value = Math.Max(max_value, arr[i]);
    }
 
    // Stores GCD of array
    int GCDArr = arr[0];
 
    // Traverse the array, arr[]
    for (int i = 1; i < N; ++i)
    {
 
      // Update GCDArr
      GCDArr = gcd(GCDArr, arr[i]);
    }
 
    // Stores distinct elements in the array by
    // repeatidely inserting absolute difference
    // of all possible pairs
    int answer = (max_value / GCDArr) + 1;
    return answer;
  }
 
  // Driver code
  static void Main()
  {
 
    // Given array arr[]
    int[] arr = { 4, 12, 16, 24 };
    int N = arr.Length;
    Console.WriteLine(DistinctValues(arr, N));
  }
}
 
// This code is contributed by susmitakundugoaldanga.


Javascript


输出:
7

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