Related Articles

# Maximize count of unique array elements by incrementing array elements by K

• Last Updated : 17 May, 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 ( = 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++

 `// C++ program for the above approach``#include ``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);` `    ``// Function Call``    ``maxDifferent(arr, N, K);` `    ``return` `0;``}`

## Java

 `// 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 M = ``new` `HashMap();` `    ``// 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> itr = M.entrySet().iterator();` `    ``int``[] ar1 = ``new` `int``[N];` `    ``// Traverse the map``    ``while` `(itr.hasNext())``    ``{``        ``Map.Entry 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`

## Python3

 `# 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`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `public` `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``    ``Dictionary<``int``,``            ``int``> M = ``new` `Dictionary<``int``,``                                     ``int``>();` `    ``// Traverse the array``    ``for``(``int` `i = 0; i < N; i++)``    ``{``        ` `        ``// Increase the counter of``        ``// the array element by 1``        ``int` `count = M.ContainsKey(arr[i]) ? M[arr[i]] : 0;``        ` `        ``if` `(count == 0)``        ``{``            ``M.Add(arr[i], 1);``        ``}``        ``else``        ``{``            ``M[arr[i]] = count + 1;``        ``}``    ``}` `    ``int``[] ar1 = ``new` `int``[N];` `    ``// Traverse the map``    ``foreach``(KeyValuePair<``int``, ``int``> Element ``in` `M)``    ``{` `        ``// Extract the current element``        ``int` `current_element = (``int``)Element.Key;` `        ``// Number of times the current``        ``// element is present in array``        ``int` `count = (``int``)Element.Value;` `        ``// 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)``        ``{``            ``int` `count = M.ContainsKey(ar1[i]) ? M[ar1[i]] : 0;           ``              ``if` `(count == 0)``              ``{``                  ``M.Add(ar1[i], 1);``            ``}``            ``else``            ``{``                ``M[ar1[i]] = count + 1;``            ``}``        ``}``    ``}` `    ``// The size of the map is the``    ``// required answer``    ``Console.WriteLine(M.Count);``}` `// 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 contributed by shikhasingrajput`

## Javascript

 ``
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up