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:
- Traverse the array and store the frequency of every array element in a Map.
-
Traverse the Map and perform the following steps:
- If the count of the current element is 1, then don’t change the array element.
- If the count of the current element is more than 1, then increase the current element by K and increase the count of (current_element + K) by 1 in the Map.
- After completing the above steps, print the size of the Map as the maximum number of unique elements in the array.
Below is the implementation of the above approach:
// 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 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 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# 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 |
<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> |
7
Time Complexity: O(N)
Auxiliary Space: O(N)