Related Articles

Related Articles

Maximize count of unique array elements by incrementing array elements by K
  • Last Updated : 18 Jan, 2021

Given an array arr[] consisting of N integers and an integer K, the task is to find the maximum number of unique elements possible by increasing any array element by K only once.

Examples: 

Input: arr[] = {0, 2, 4, 3, 4}, K = 1
Output: 5
Explanation:
Increase arr[2] ( = 4) by K ( = 1). Therefore, new array is {0, 2, 4, 3, 5} which has 5 unique elements.

Input: arr[] = {2, 3, 2, 4, 5, 5, 7, 4}, K = 2
Output: 7
Explanation: 
Increase 4 by 2 = 6. 
Increase element 7 by 2 = 9
Increase 5 by 2 = 7. 
The new array is {2, 3, 2, 4, 5, 7, 9, 6} which contains 7 unique elements.

Approach: The idea to solve this problem is to store the frequency of elements of the array in a Map and change the array elements accordingly to get unique elements in the array after incrementing any values by K. Follow the steps below to solve the problem:



Below is the implementation of the above approach: 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the maximum unique
// elements in array after incrementing
// any element by K
void maxDifferent(int arr[], int N, int K)
{
    // Stores the count of element
    // in array
    map<int, int> M;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Increase the counter of
        // the array element by 1
        M[arr[i]]++;
    }
 
    // Traverse the map
    for (auto it = M.begin();
         it != M.end(); it++) {
 
        // Extract the current element
        int current_element = it->first;
 
        // Number of times the current
        // element is present in array
        int count = it->second;
 
        // If element is present only
        // once, then do not change it
        if (count == 1)
            continue;
 
        // If the count > 1 then change
        // one of the same current
        // elements to (current_element + K)
        // and increase its count by 1
        M[current_element + K]++;
    }
 
    // The size of the map is the
    // required answer
    cout << M.size();
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 3, 2, 4, 5, 5, 7, 4 };
    int K = 2;
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    maxDifferent(arr, N, K);
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to find the maximum unique
// elements in array after incrementing
// any element by K
static void maxDifferent(int arr[], int N, int K)
{
     
    // Stores the count of element
    // in array
    HashMap<Integer,
            Integer> M = new HashMap<Integer,
                                     Integer>();
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // Increase the counter of
        // the array element by 1
        Integer count = M.get(arr[i]);
         
        if (count == null)
        {
            M.put(arr[i], 1);
        }
        else
        {
            M.put(arr[i], count + 1);
        }
    }
 
    // Iterator itr = M.entrySet().iterator();
    Iterator<Map.Entry<Integer,
                       Integer>> itr = M.entrySet().iterator();
 
    int[] ar1 = new int[N];
 
    // Traverse the map
    while (itr.hasNext())
    {
        Map.Entry<Integer, Integer> Element = itr.next();
 
        // Extract the current element
        int current_element = (int)Element.getKey();
 
        // Number of times the current
        // element is present in array
        int count = (int)Element.getValue();
 
        // If element is present only
        // once, then do not change it
        if (count == 1)
            continue;
 
        // If the count > 1 then change
        // one of the same current
        // elements to (current_element + K)
        // and increase its count by 1
        ar1[current_element + K]++;
    }
 
    for(int i = 0; i < N; i++)
    {
        if (ar1[i] >= 0)
        {
            Integer count = M.get(ar1[i]);
             
              if (count == null)
              {
                  M.put(ar1[i], 1);
            }
            else
            {
                M.put(ar1[i], count + 1);
            }
        }
    }
 
    // The size of the map is the
    // required answer
    System.out.println(M.size());
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 2, 3, 2, 4, 5, 5, 7, 4 };
    int K = 2;
    int N = arr.length;
     
    // Function Call
    maxDifferent(arr, N, K);
}
}
 
// This code is contributed by Dharanendra L V

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
 
# Function to find the maximum unique
# elements in array after incrementing
# any element by K
def maxDifferent(arr, N, K):
     
    # Stores the count of element
    # in array
    M = {}
 
    # Traverse the array
    for i in range(N):
 
        # Increase the counter of
        # the array element by 1
        M[arr[i]] = M.get(arr[i], 0) + 1
 
    # Traverse the map
    for it in list(M.keys()):
 
        # Extract the current element
        current_element = it
 
        # Number of times the current
        # element is present in array
        count = M[it]
 
        # If element is present only
        # once, then do not change it
        if (count == 1):
            continue
 
        # If the count > 1 then change
        # one of the same current
        # elements to (current_element + K)
        # and increase its count by 1
        M[current_element + K] = M.get(current_element, 0) + 1
 
    # The size of the map is the
    # required answer
    print(len(M))
 
# Driver Code
if __name__ == '__main__':
    arr=[2, 3, 2, 4, 5, 5, 7, 4]
    K = 2
    N = len(arr)
 
    # Function Call
    maxDifferent(arr, N, K)
 
    # This code is contributed by mohit kumar 29

chevron_right


Output: 

7

 

Time Complexity: O(N)
Auxiliary Space: O(N)

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :