Skip to content
Related Articles

Related Articles

Minimize length of an array consisting of difference between all possible pairs
  • Last Updated : 14 Jan, 2021

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:
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:
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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 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);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Output: 

3

 

Time Complexity:O(N * Log(X)), where X is the largest element in the array. 
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :