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

📅  最后修改于: 2021-09-04 11:40:02             🧑  作者: 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)

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