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

