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[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++
// 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;
}
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<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
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
<script>
// JavaScript program for the above approach
// Function to find the maximum unique
// elements in array after incrementing
// any element by K
function maxDifferent(arr, N, K)
{
// Stores the count of element
// in array
var M = new Map();
// Traverse the array
for (var i = 0; i < N; i++) {
// Increase the counter of
// the array element by 1
if(M.has(arr[i]))
{
M.set(arr[i], M.get(arr[i])+1);
}
else
{
M.set(arr[i],1);
}
}
M.forEach((value, key) => {
// Extract the current element
var current_element = key;
// Number of times the current
// element is present in array
var count = value;
// If element is present only
// once, then do not change it
if (count != 1)
{
// If the count > 1 then change
// one of the same current
// elements to (current_element + K)
// and increase its count by 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);
}
}
});
// The size of the map is the
// required answer
document.write( M.size);
}
// Driver Code
var arr = [2, 3, 2, 4, 5, 5, 7, 4];
var K = 2;
var N = arr.length;
// Function Call
maxDifferent(arr, N, K);
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Maximize count of K unique elements that can be chosen from Array Given an arrays arr[] of size N and an array of queries Q[] of size M, where Q[i] defines the count of unique elements that have to be chosen from the array arr[]. The task to find the maximum number of elements that can be picked for each query. Examples: Input: arr[ ] = {30, 31, 32, 33, 32, 32, 31
6 min read
Maximize equal elements in two Arrays after at most K increments Given two arrays arr1[] and arr2[] of length N each and an integer K, The task is to maximize the number of equal elements at the same index in arr1[] and arr2[] by incrementing any element of arr2[] but the total increment must be at most K. Examples: Input: arr1[] = {4, 5, 6, 7}, arr2[] = {3, 4, 5
6 min read
Count of Unique elements after inserting average of MEX and Array Max K times Given an array A[] of N non-negative integers and an integer K. Each time, you can do the following two operations Find Average of MAX and MEX(rounded up to closest greater integer) of the array.Insert calculated average in the array. After performing the above operation K times, find the count of u
9 min read
Maximize count of distinct elements in a subsequence of size K in given array Given an array arr[] of N integers and an integer K, the task is to find the maximum count of distinct elements over all the subsequences of K integers. Example: Input: arr[]={1, 1, 2, 2}, K=3Output: 2Explanation: The subsequence {1, 1, 2} has 3 integers and the number of distinct integers in it are
4 min read
Maximize distinct elements by incrementing/decrementing an element or keeping it same Given an array arr[] of N elements, the task is to maximize the count of distinct elements in the array, by either of the given operation on each element of the array: either increasing the element by 1or decreasing the element by 1or keeping the element as it is. Note: No element can be less than o
6 min read