Minimize distinct elements in Array after replacing arr[i] with arr[i] modulo X
Last Updated :
14 Oct, 2022
Given an array arr[] of size N, the task is to minimize the number of distinct elements in the array after performing the following operation once:
- Choose an integer X and replace each element of the array with the remainder when arr[i] is divided by X (i.e., arr[i]%X).
Examples:
Input: N = 5, arr[] = {5, 10, 20, 35, 15}
Output: 1
Explanation: Lets take X = 5. On replacing each element
as mentioned above we get arr = {0, 0, 0, 0, 0}.
Now this array has only one distinct element (0).
Input: N = 4, arr[] = {1, 4, 8}
Output: 2
Approach: To solve the problem follow the below observation:
The answer would be at – most 2.
This would be the case when we choose X = 2. The final array will contain only either 0 (for even numbers) or 1 (for odd numbers).
So, now the task is to check for the condition when the answer would be 1.
We want to find X, such that A[1] % X = A[2] % X, A[2] % X = A[3] % X, and so on for the whole array.
Therefore, (A[1] – A[2]) % X = 0 and (A[2] – A[3]) % X = 0 and so on for the whole array.
Therefore, we need to find an X, such that all the consecutive differences are divisible by it.
So, the following steps can be followed to solve the problem:
- Initialize a variable gcd by 0.
- Iterate through the array from 0 to N-1.
- At each iteration take gcd of the variable gcd and difference between arr[i] and arr[i + 1].
- At the end of the iteration, if gcd is 1, return 2.
- Else, return 1.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int minimumDistinct( int N, int A[])
{
int gcd = 0;
for ( int i = 0; i < N - 1; i++) {
gcd = __gcd(gcd, abs (A[i] - A[i + 1]));
}
if (gcd == 1) {
return 2;
}
return 1;
}
int main()
{
int arr[] = { 5, 10, 20, 35, 15 };
int N = sizeof (arr) / sizeof (arr[0]);
int answer = minimumDistinct(N, arr);
cout << answer << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int GCD( int a, int b)
{
if (b == 0 )
return a;
else
return GCD(b, a % b);
}
public static int minimumDistinct( int N, int A[])
{
int gcd = 0 ;
for ( int i = 0 ; i < N - 1 ; i++) {
gcd = GCD(gcd, Math.abs(A[i] - A[i + 1 ]));
}
if (gcd == 1 ) {
return 2 ;
}
return 1 ;
}
public static void main(String[] args)
{
int arr[] = { 5 , 10 , 20 , 35 , 15 };
int N = arr.length;
int answer = minimumDistinct(N, arr);
System.out.println(answer);
}
}
|
Python3
def GCD(a, b):
if (b = = 0 ):
return a
else :
return GCD(b, a % b)
def minimumDistinct(N, A):
gcd = 0
for i in range ( 0 , N - 1 ):
gcd = GCD(gcd, abs (A[i] - A[i + 1 ]))
if (gcd = = 1 ):
return 2
return 1
if __name__ = = '__main__' :
arr = [ 5 , 10 , 20 , 35 , 15 ]
N = len (arr)
answer = minimumDistinct(N, arr)
print (answer)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static int GCD( int a, int b)
{
if (b == 0)
return a;
else
return GCD(b, a % b);
}
public static int minimumDistinct( int N, int [] A)
{
int gcd = 0;
for ( int i = 0; i < N - 1; i++) {
gcd = GCD(gcd, Math.Abs(A[i] - A[i + 1]));
}
if (gcd == 1) {
return 2;
}
return 1;
}
public static void Main()
{
int [] arr = { 5, 10, 20, 35, 15 };
int N = arr.Length;
int answer = minimumDistinct(N, arr);
Console.WriteLine(answer);
}
}
|
Javascript
<script>
function __gcd(a, b) {
return b == 0 ? a : __gcd(b, a % b);
}
function minimumDistinct(N, A) {
let gcd = 0;
for (let i = 0; i < N - 1; i++) {
gcd = __gcd(gcd, Math.abs(A[i] - A[i + 1]));
}
if (gcd == 1) {
return 2;
}
return 1;
}
let arr = [5, 10, 20, 35, 15];
let N = arr.length;
let answer = minimumDistinct(N, arr);
document.write(answer);
</script>
|
Time Complexity: O(N * log(M)) where M is the maximum value of the array
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...