Count Non-Repeating array elements after inserting absolute difference between all possible pairs
Last Updated :
29 Nov, 2022
Given an array arr[] of size N, the task is to maximize the count of distinct array elements by repeatedly inserting the absolute difference between all possible pairs of the given array.
Examples:
Input: arr[] = { 2, 4, 16 }
Output: 9
Explanation:
Inserting (arr[2] – arr[1]) modifies arr[] to { 2, 4, 12, 16 }
Inserting (arr[2] – arr[1]) modifies arr[] to { 2, 4, 8, 12, 16 }
Inserting (arr[2] – arr[1]) modifies arr[] to { 2, 4, 6, 8, 12, 16 }
Inserting (arr[4] – arr[0]) modifies arr[] to { 2, 4, 6, 8, 10, 12, 16 }
Inserting (arr[6] – arr[0]) modifies arr[] to { 2, 4, 6, 8, 10, 12, 14 16 }
Inserting (arr[2] – arr[0]) modifies arr[] to { 2, 4, 4 6, 8, 10, 12, 14 16 }
Inserting (arr[2] – arr[1]) modifies arr[] to { 0, 2, 4, 4 6, 8, 10, 12, 14 16 }
Therefore, the required output is 9.
Input: arr[] = { 3, 6, 5, 4 }
Output: 7
Naive Approach: The simplest approach to solve this problem is to repeatedly select a pair from the given array and insert the absolute difference of that pair. Finally, check if the absolute difference of all possible pairs is already present in the array or not. If found to be true, then print the count of distinct elements into the array.
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: The idea is to use the fact that the GCD of two numbers can be obtained by repeatedly subtracting the smaller number from the bigger number until two elements become equal. Therefore, insert the elements into the array such that the absolute difference between all the adjacent elements must be equal to the GCD of the 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 gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
int DistinctValues( int arr[], int N)
{
int max_value = INT_MIN;
for ( int i = 0; i < N; ++i) {
max_value = max(max_value, arr[i]);
}
int GCDArr = arr[0];
for ( int i = 1; i < N; ++i) {
GCDArr = gcd(GCDArr, arr[i]);
}
int answer = (max_value / GCDArr) + 1;
return answer;
}
int main()
{
int arr[] = { 4, 12, 16, 24 };
int N = sizeof (arr) / sizeof ( int );
cout << DistinctValues(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static int DistinctValues( int arr[], int N)
{
int max_value = Integer.MIN_VALUE;
for ( int i = 0 ; i < N; ++i)
{
max_value = Math.max(max_value, arr[i]);
}
int GCDArr = arr[ 0 ];
for ( int i = 1 ; i < N; ++i)
{
GCDArr = gcd(GCDArr, arr[i]);
}
int answer = (max_value / GCDArr) + 1 ;
return answer;
}
public static void main(String[] args)
{
int arr[] = { 4 , 12 , 16 , 24 };
int N = arr.length;
System.out.println(DistinctValues(arr, N));
}
}
|
Python3
import sys
def gcd(a, b):
if a = = 0 :
return b
return gcd(b % a, a)
def DistinctValues(arr, N):
max_value = - sys.maxsize - 1
max_value = max (arr)
GCDArr = arr[ 0 ]
for i in range ( 1 , N):
GCDArr = gcd(GCDArr, arr[i])
answer = max_value / / GCDArr
return answer + 1
arr = [ 4 , 12 , 16 , 24 ]
N = len (arr)
print (DistinctValues(arr, N))
|
C#
using System;
class GFG
{
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int DistinctValues( int [] arr, int N)
{
int max_value = Int32.MinValue;
for ( int i = 0; i < N; ++i)
{
max_value = Math.Max(max_value, arr[i]);
}
int GCDArr = arr[0];
for ( int i = 1; i < N; ++i)
{
GCDArr = gcd(GCDArr, arr[i]);
}
int answer = (max_value / GCDArr) + 1;
return answer;
}
static void Main()
{
int [] arr = { 4, 12, 16, 24 };
int N = arr.Length;
Console.WriteLine(DistinctValues(arr, N));
}
}
|
Javascript
<script>
function gcd(a , b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
function DistinctValues(arr , N) {
var max_value = Number.MIN_VALUE;
for (i = 0; i < N; ++i) {
max_value = Math.max(max_value, arr[i]);
}
var GCDArr = arr[0];
for (i = 1; i < N; ++i) {
GCDArr = gcd(GCDArr, arr[i]);
}
var answer = (max_value / GCDArr) + 1;
return answer;
}
var arr = [ 4, 12, 16, 24 ];
var N = arr.length;
document.write(DistinctValues(arr, N));
</script>
|
Time complexity: O(N * Min), where Min is the smallest element of the array
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...