给定一个大小为N的数组arr[] ,任务是找到需要插入到数组中的最小元素数,使得数组中存在所有可能对的绝对差异。
例子:
Input: arr[] = { 3, 5 }
Output: 3
Explanation:
Inserting 2 into the array modifies arr[] to { 2, 3, 5 }
Inserting 1 into the array modifies arr[] to { 1, 2, 3, 5 }
Inserting 4 into the array modifies arr[] to { 1, 2, 3, 4, 5 }
Since absolute difference of all possible pairs from the array are present in the array, the required output is 3.
Input: arr[] = { 2, 4 }
Output: 0
Explanation:
Since absolute difference of all possible pairs array are already present in the array, the required output is 0.
朴素的方法:解决这个问题的最简单的方法是从数组中找到每个可能对的绝对差异,并检查获得的差异是否存在于数组中。如果不存在,则插入获得的差异。否则,打印插入数组的元素计数。
时间复杂度: O(N * X),其中 X 是数组中的最大元素。
辅助空间: O(X)
高效的方法:上述方法可以基于以下观察进行优化:
Since the absolute difference of all possible pairs of final array must be present in the array.
Therefore, the final array must be in the form of X, 2 * X, 3 * X, 4 * X, …
From the above sequence of the final array, it can be observed the value of X must be the GCD of the given array.
请按照以下步骤解决问题:
- 遍历数组并计算给定数组的 GCD,例如X 。
- 找到数组的最大元素,比如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.
Javascript
3
时间复杂度: O(N * Log(X)),其中 X 是数组中最大的元素。
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live