Given an array arr[] of size N, the task is to find the minimum count of elements required to be inserted into the array such that the absolute difference of all possible pairs exists in the array.
Examples:
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.
Naive Approach: The simplest approach to solve this problem is to find the absolute difference of every possible pair from the array and check if the difference obtained is present in the array or not. If it is not present, then insert the obtained difference. Otherwise, print the count of elements inserted into the array.
Time Complexity:O(N * X), where X is the maximum element in the array.
Auxiliary Space: O(X)
Efficient Approach: The above approach can be optimized based on the following observations:
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.
Follow the steps below to solve the problem:
- Traverse the array and calculate the GCD of the given array say, X.
- Find the largest element of the array say, Max.
- Total count of elements in the array by inserting the absolute difference of all possible pairs is (Max) / X.
- Therefore, the total count of elements inserted into the array is equal to ((Max / X) – N).
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> 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 element
// 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 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 element
// 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 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 element
# 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# 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 element
// 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. |
<script> // Javascript program for the above approach // Recursive function to return gcd of a and b function gcdd(a, 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 function findGcd(arr, N)
{ // Stores GCD of an array
var gcd = arr[0];
// Traverse the array
for (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 function findMax(arr, N)
{ // Stores gcd of the array
var gcd = findGcd(arr, N);
// Stores the largest element
// in the array
var Max = Number.MIN_VALUE;
// Traverse the array
for (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
var ans = (Max / gcd) - N;
document.write(ans);
} // Driver code // Given array var arr = [ 3, 5 ];
// Size of the array var N = arr.length;
// Function Call findMax(arr, N); // This code is contributed by umadevi9616 </script> |
3
Time Complexity:O(N * Log(X)), where X is the largest element in the array.
Auxiliary Space: O(1)