Maximize count of distinct elements possible in an Array from the given operation
Last Updated :
11 May, 2021
Given an array A[] of size N, the task is to maximize the count of distinct elements in the array by inserting the absolute differences of the existing array elements.
Examples:
Input: A[] = {1, 2, 3, 5}
Output: 5
Explanation:
Possible absolute differences among the array elements are:
(2 – 1) = 1
(5 – 3) = 2
(5 – 2) = 3
(5 – 1) = 4
Hence, inserting 4 into the array maximizes the count of distinct elements.
Input: A[] = {1, 2, 3, 6}
Output: 6
Naive Approach:
Generate all possible pairs from the array and store their absolute differences in a set. Insert all the array elements into the set. The final size of the set denotes the maximum distinct elements that the array can possess after performing given operations.
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach:
Follow the steps below to optimize the above approach:
- Find the maximum element of the array.The maximum possible absolute difference of any two elements does not exceed the maximum element of the array.
- Calculate the GCD of the array, since it is the common factor of the whole array.
- Divide the maximum element with the gcd of the array to get the count of the distinct elements.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int x, int y)
{
if (x == 0)
return y;
return gcd(y % x, x);
}
int findDistinct( int arr[], int n)
{
int maximum = *max_element(arr,
arr + n);
if (n == 1)
return 1;
if (n == 2) {
return (maximum / gcd(arr[0],
arr[1]));
}
int k = gcd(arr[0], arr[1]);
for ( int i = 2; i < n; i++) {
k = gcd(k, arr[i]);
}
return (maximum / k);
}
int main()
{
int arr[] = { 1, 2, 3, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << findDistinct(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int gcd( int x, int y)
{
if (x == 0 )
return y;
return gcd(y % x, x);
}
static int findDistinct( int arr[], int n)
{
int maximum = Arrays.stream(arr).max().getAsInt();
if (n == 1 )
return 1 ;
if (n == 2 )
{
return (maximum / gcd(arr[ 0 ],
arr[ 1 ]));
}
int k = gcd(arr[ 0 ], arr[ 1 ]);
for ( int i = 2 ; i < n; i++)
{
k = gcd(k, arr[i]);
}
return (maximum / k);
}
public static void main (String[] args)
{
int arr[] = { 1 , 2 , 3 , 5 };
int n = arr.length;
System.out.println(findDistinct(arr, n));
}
}
|
Python3
def gcd(x, y):
if (x = = 0 ):
return y
return gcd(y % x, x)
def findDistinct(arr, n):
maximum = max (arr)
if (n = = 1 ):
return 1
if (n = = 2 ):
return (maximum / / gcd(arr[ 0 ],
arr[ 1 ]))
k = gcd(arr[ 0 ], arr[ 1 ])
for i in range ( 2 , n):
k = gcd(k, arr[i])
return (maximum / / k)
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 5 ]
n = len (arr)
print (findDistinct(arr, n))
|
C#
using System;
using System.Linq;
class GFG{
static int gcd( int x, int y)
{
if (x == 0)
return y;
return gcd(y % x, x);
}
static int findDistinct( int []arr, int n)
{
int maximum = arr.Max();
if (n == 1)
return 1;
if (n == 2)
{
return (maximum / gcd(arr[0],
arr[1]));
}
int k = gcd(arr[0], arr[1]);
for ( int i = 2; i < n; i++)
{
k = gcd(k, arr[i]);
}
return (maximum / k);
}
public static void Main()
{
int []arr = { 1, 2, 3, 5 };
int n = arr.Length;
Console.Write(findDistinct(arr, n));
}
}
|
Javascript
<script>
function gcd(x, y)
{
if (x == 0)
return y;
return gcd(y % x, x);
}
function findDistinct(arr, n)
{
let maximum = Math.max(...arr);
if (n == 1)
return 1;
if (n == 2)
{
return (maximum / gcd(arr[0],
arr[1]));
}
let k = gcd(arr[0], arr[1]);
for (let i = 2; i < n; i++)
{
k = gcd(k, arr[i]);
}
return (maximum / k);
}
let arr = [ 1, 2, 3, 5 ];
let n = arr.length;
document.write(findDistinct(arr, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...