Open In App

Minimize distinct elements in Array after replacing arr[i] with arr[i] modulo X

Last Updated : 14 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to  minimize the number of
// distinct elements  in the array after
// performing the given operation
int minimumDistinct(int N, int A[])
{
    // Initialize gcd by 0
    int gcd = 0;
 
    // Iterate through the array and
    // calculate gcd of all the consecutive
    // differences of array elements
    for (int i = 0; i < N - 1; i++) {
        gcd = __gcd(gcd, abs(A[i] - A[i + 1]));
    }
 
    // If gcd is 1, return 2
    if (gcd == 1) {
        return 2;
    }
 
    // Else return 1
    return 1;
}
 
// Driver code
int main()
{
    int arr[] = { 5, 10, 20, 35, 15 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    int answer = minimumDistinct(N, arr);
    cout << answer << endl;
    return 0;
}


Java




// Java code to implement the approach
import java.io.*;
 
class GFG {
    public static int GCD(int a, int b)
    {
        if (b == 0)
            return a;
        else
            return GCD(b, a % b);
    }
    // Function to  minimize the number of
    // distinct elements in the array after
    // performing the given operation
    public static int minimumDistinct(int N, int A[])
    {
        // Initialize gcd by 0
        int gcd = 0;
 
        // Iterate through the array and
        // calculate gcd of all the consecutive
        // differences of array elements
        for (int i = 0; i < N - 1; i++) {
            gcd = GCD(gcd, Math.abs(A[i] - A[i + 1]));
        }
 
        // If gcd is 1, return 2
        if (gcd == 1) {
            return 2;
        }
 
        // Else return 1
        return 1;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 5, 10, 20, 35, 15 };
        int N = arr.length;
 
        // Function Call
        int answer = minimumDistinct(N, arr);
        System.out.println(answer);
    }
}
 
// This code is contributed by Rohit Pradhan


Python3




# Python code to implement the approach
 
# Function to find gcd
def GCD(a, b):
 
    if (b == 0):
        return a
    else:
        return GCD(b, a % b)
       
# Function to  minimize the number of
# distinct elements  in the array after
# performing the given operation
def minimumDistinct(N, A):
   
    # Initialize gcd by 0
    gcd = 0
 
    # Iterate through the array and
    # calculate gcd of all the consecutive
    # differences of array elements
    for i in range(0, N-1):
        gcd = GCD(gcd, abs(A[i] - A[i + 1]))
 
    # If gcd is 1, return 2
    if (gcd == 1):
        return 2
 
    # Else return 1
    return 1
 
# Driver Code
if __name__ == '__main__':
    arr = [5, 10, 20, 35, 15]
    N = len(arr)
 
    # Function call
    answer = minimumDistinct(N, arr)
    print(answer)
 
    # This code is contributed by aarohirai2616.


C#




// C# code to implement the approach
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);
  }
  // Function to  minimize the number of
  // distinct elements in the array after
  // performing the given operation
  public static int minimumDistinct(int N, int[] A)
  {
    // Initialize gcd by 0
    int gcd = 0;
 
    // Iterate through the array and
    // calculate gcd of all the consecutive
    // differences of array elements
    for (int i = 0; i < N - 1; i++) {
      gcd = GCD(gcd, Math.Abs(A[i] - A[i + 1]));
    }
 
    // If gcd is 1, return 2
    if (gcd == 1) {
      return 2;
    }
 
    // Else return 1
    return 1;
  }
 
  // Driver Code
  public static void Main()
  {
    int[] arr = { 5, 10, 20, 35, 15 };
    int N = arr.Length;
 
    // Function Call
    int answer = minimumDistinct(N, arr);
    Console.WriteLine(answer);
  }
}
 
// This code is contributed by code_hunt.


Javascript




<script>
       // JavaScript code for the above approach
 
       function __gcd(a, b) {
           return b == 0 ? a : __gcd(b, a % b);
       }
       // Function to  minimize the number of
       // distinct elements  in the array after
       // performing the given operation
       function minimumDistinct(N, A) {
           // Initialize gcd by 0
           let gcd = 0;
 
           // Iterate through the array and
           // calculate gcd of all the consecutive
           // differences of array elements
           for (let i = 0; i < N - 1; i++) {
               gcd = __gcd(gcd, Math.abs(A[i] - A[i + 1]));
           }
 
           // If gcd is 1, return 2
           if (gcd == 1) {
               return 2;
           }
 
           // Else return 1
           return 1;
       }
 
       // Driver code
 
       let arr = [5, 10, 20, 35, 15];
       let N = arr.length;
 
       // Function Call
       let answer = minimumDistinct(N, arr);
       document.write(answer);
 
// This code is contributed by Potta Lokesh
 
   </script>


Output

1

Time Complexity: O(N * log(M)) where M is the maximum value of the array
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads