Minimize length of an array consisting of difference between all possible pairs
Last Updated :
11 Feb, 2022
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:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findGcd( int arr[], int N)
{
int gcd = arr[0];
for ( int i = 1; i < N; i++) {
gcd = __gcd(gcd, arr[i]);
}
return gcd;
}
void findMax( int arr[], int N)
{
int gcd = findGcd(arr, N);
int Max = INT_MIN;
for ( int i = 0; i < N; i++) {
Max = max(Max, arr[i]);
}
int ans = (Max / gcd) - N;
cout << ans;
}
int main()
{
int arr[] = { 3, 5 };
int N = ( sizeof (arr) / ( sizeof (arr[0])));
findMax(arr, N);
}
|
Java
import java.util.*;
class GFG
{
static int gcdd( int a, int b)
{
if (a == 0 )
return b;
if (b == 0 )
return a;
if (a == b)
return a;
if (a > b)
return gcdd(a - b, b);
return gcdd(a, b - a);
}
static int findGcd( int arr[], int N)
{
int gcd = arr[ 0 ];
for ( int i = 1 ; i < N; i++)
{
gcd = gcdd(gcd, arr[i]);
}
return gcd;
}
static void findMax( int arr[], int N)
{
int gcd = findGcd(arr, N);
int Max = Integer.MIN_VALUE;
for ( int i = 0 ; i < N; i++)
{
Max = Math.max(Max, arr[i]);
}
int ans = (Max / gcd) - N;
System.out.println(ans);
}
public static void main(String[] args)
{
int arr[] = { 3 , 5 };
int N = arr.length;
findMax(arr, N);
}
}
|
Python3
import math
import sys
def findGcd(arr, N):
gcd = arr[ 0 ]
for i in range ( 1 , N):
gcd = math.gcd(gcd, arr[i])
return gcd
def findMax(arr, N):
gcd = findGcd(arr, N)
Max = - sys.maxsize - 1
for i in range (N):
Max = max ( Max , arr[i])
ans = ( Max / / gcd) - N
print (ans)
if __name__ = = "__main__" :
arr = [ 3 , 5 ]
N = len (arr)
findMax(arr, N)
|
C#
using System;
class GFG
{
static int gcdd( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcdd(a - b, b);
return gcdd(a, b - a);
}
static int findGcd( int [] arr, int N)
{
int gcd = arr[0];
for ( int i = 1; i < N; i++)
{
gcd = gcdd(gcd, arr[i]);
}
return gcd;
}
static void findMax( int [] arr, int N)
{
int gcd = findGcd(arr, N);
int Max = Int32.MinValue;
for ( int i = 0; i < N; i++)
{
Max = Math.Max(Max, arr[i]);
}
int ans = (Max / gcd) - N;
Console.WriteLine(ans);
}
static public void Main()
{
int [] arr = new int [] { 3, 5 };
int N = arr.Length;
findMax(arr, N);
}
}
|
Javascript
<script>
function gcdd(a, b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcdd(a - b, b);
return gcdd(a, b - a);
}
function findGcd(arr, N)
{
var gcd = arr[0];
for (i = 1; i < N; i++)
{
gcd = gcdd(gcd, arr[i]);
}
return gcd;
}
function findMax(arr, N)
{
var gcd = findGcd(arr, N);
var Max = Number.MIN_VALUE;
for (i = 0; i < N; i++)
{
Max = Math.max(Max, arr[i]);
}
var ans = (Max / gcd) - N;
document.write(ans);
}
var arr = [ 3, 5 ];
var N = arr.length;
findMax(arr, N);
</script>
|
Time Complexity:O(N * Log(X)), where X is the largest element in the array.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...