Maximize count of unique array elements by incrementing array elements by K
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++
#include <bits/stdc++.h>
using namespace std;
void maxDifferent( int arr[], int N, int K)
{
map< int , int > M;
for ( int i = 0; i < N; i++) {
M[arr[i]]++;
}
for ( auto it = M.begin();
it != M.end(); it++) {
int current_element = it->first;
int count = it->second;
if (count == 1)
continue ;
M[current_element + K]++;
}
cout << M.size();
}
int main()
{
int arr[] = { 2, 3, 2, 4, 5, 5, 7, 4 };
int K = 2;
int N = sizeof (arr) / sizeof (arr[0]);
maxDifferent(arr, N, K);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void maxDifferent( int arr[], int N, int K)
{
HashMap<Integer,
Integer> M = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < N; i++)
{
Integer count = M.get(arr[i]);
if (count == null )
{
M.put(arr[i], 1 );
}
else
{
M.put(arr[i], count + 1 );
}
}
Iterator<Map.Entry<Integer,
Integer>> itr = M.entrySet().iterator();
int [] ar1 = new int [N];
while (itr.hasNext())
{
Map.Entry<Integer, Integer> Element = itr.next();
int current_element = ( int )Element.getKey();
int count = ( int )Element.getValue();
if (count == 1 )
continue ;
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 );
}
}
}
System.out.println(M.size());
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 2 , 4 , 5 , 5 , 7 , 4 };
int K = 2 ;
int N = arr.length;
maxDifferent(arr, N, K);
}
}
|
Python3
def maxDifferent(arr, N, K):
M = {}
for i in range (N):
M[arr[i]] = M.get(arr[i], 0 ) + 1
for it in list (M.keys()):
current_element = it
count = M[it]
if (count = = 1 ):
continue
M[current_element + K] = M.get(current_element, 0 ) + 1
print ( len (M))
if __name__ = = '__main__' :
arr = [ 2 , 3 , 2 , 4 , 5 , 5 , 7 , 4 ]
K = 2
N = len (arr)
maxDifferent(arr, N, K)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static void maxDifferent( int []arr, int N, int K)
{
Dictionary< int ,
int > M = new Dictionary< int ,
int >();
for ( int i = 0; i < N; i++)
{
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];
foreach (KeyValuePair< int , int > Element in M)
{
int current_element = ( int )Element.Key;
int count = ( int )Element.Value;
if (count == 1)
continue ;
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;
}
}
}
Console.WriteLine(M.Count);
}
public static void Main(String[] args)
{
int []arr = { 2, 3, 2, 4, 5, 5, 7, 4 };
int K = 2;
int N = arr.Length;
maxDifferent(arr, N, K);
}
}
|
Javascript
<script>
function maxDifferent(arr, N, K)
{
var M = new Map();
for ( var i = 0; i < N; i++) {
if (M.has(arr[i]))
{
M.set(arr[i], M.get(arr[i])+1);
}
else
{
M.set(arr[i],1);
}
}
M.forEach((value, key) => {
var current_element = key;
var count = value;
if (count != 1)
{
if (M.has(current_element+K))
{
M.set(current_element+K,
M.get(current_element+K)+1)
}
else
{
M.set(current_element+K,1);
}
}
});
document.write( M.size);
}
var arr = [2, 3, 2, 4, 5, 5, 7, 4];
var K = 2;
var N = arr.length;
maxDifferent(arr, N, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
17 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...