Given an array arr[] of size N and every element in the array arr[] is in the range [1, N] and the array may contain duplicates. The task is to find the maximum number of unique values that can be obtained such that the value at any index i can either be:
- Increased by 1.
- Decreased by 1.
- Left as it is.
Note: The operation can be performed only once with each index and have to be performed for all the indices in the array arr[].
Examples:
Input: arr[] = {1, 2, 4, 4}
Output: 4
Explanation:
One way is to perform the following operations for the index:
1: Leave the value at the first index(1) as it is.
2: Leave the value at the second index(2) as it is.
3: Leave the value at the third index(4) as it is.
4: Increment the value at the fourth index(4) by 1.
Then the array becomes {1, 2, 4, 5} and there are 4 unique values.
Input: arr[]={3, 3, 3, 3}
Output: 3
Explanation:
One way is to perform the following operations for the index:
1: Leave the value at the first index(3) as it is.
2: Decrement the value at the second index(3) by 1.
3: Leave the value at the third index(3) as it is.
4: Increment the value at the fourth index(3) by 1.
Then the array becomes {3, 2, 3, 4} and there are 3 unique values.
Approach:
- For some arbitrary element X present in the array at index i, we decide what operation to perform on it by taking the following things into consideration:
- We decrement the value X by 1 if the value (X – 1) is not present in the array and there are one or more other X’s present in the array at different indices.
- We don’t change the value X if the value X is present only once on the array.
- We increment the value X by 1 if the value (X + 1) is not present in the array and there are one or more other X’s present in the array at different indices.
- By taking the above decisions for every element, we can be sure that the final count of unique elements which we get is the maximum.
- However, to perform the above steps for every index and count the occurrences of the element X and continuously update the array arr[], the time taken would be quadratic which is not feasible for large-sized arrays.
- One alternative to reduce the time complexity is to initially sort the array. By sorting, all the elements in the array are grouped and all the repeated values come together.
- After sorting the array, since the range of the numbers is already given and it is fixed, a hash map can be used where the keys of the hash are the numbers in the range [1, N] and the value for each key is boolean which is used to determine if the key is present in the array or not.
- In this problem, since the indices themselves are the keys of the hash, an array freq[] of size (N + 2) is used to implement the hash.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int uniqueNumbers(vector< int > arr, int n)
{
sort(arr.begin(),arr.end());
vector< int > freq(n+2,0) ;
for ( int x = 0; x < n; x++) {
if (freq[arr[x] - 1] == 0) {
freq[arr[x] - 1]++;
}
else if (freq[arr[x]] == 0) {
freq[arr[x]]++;
}
else {
freq[arr[x] + 1]++;
}
}
int unique = 0;
for ( int x = 0; x <= n + 1; x++) {
if (freq[x]) {
unique++;
}
}
return unique;
}
int main()
{
vector< int > arr= { 3, 3, 3, 3 };
int n = arr.size();
int ans = uniqueNumbers(arr, n);
cout << ans;
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
int cmpfunc ( const void * a, const void * b) {
return ( *( int *)a - *( int *)b );}
int uniqueNumbers( int * arr, int n)
{
qsort (arr, n, sizeof ( int ), cmpfunc);
int freq[n+2];
for ( int i = 0 ; i < n + 2 ; i++)
freq[i] = 0;
for ( int x = 0; x < n; x++) {
if (freq[arr[x] - 1] == 0) {
freq[arr[x] - 1]++;
}
else if (freq[arr[x]] == 0) {
freq[arr[x]]++;
}
else {
freq[arr[x] + 1]++;
}
}
int unique = 0;
for ( int x = 0; x <= n + 1; x++) {
if (freq[x]) {
unique++;
}
}
return unique;
}
int main()
{
int arr[] = { 3, 3, 3, 3 };
int n = sizeof (arr)/ sizeof (arr[0]);
int ans = uniqueNumbers(arr, n);
printf ( "%d" , ans);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int uniqueNumbers( int arr[], int n)
{
Arrays.sort(arr);
int freq[] = new int [n + 2 ];
for ( int i = 0 ; i < n + 2 ; i++)
freq[i] = 0 ;
for ( int x = 0 ; x < n; x++) {
if (freq[arr[x] - 1 ] == 0 ) {
freq[arr[x] - 1 ]++;
}
else if (freq[arr[x]] == 0 ) {
freq[arr[x]]++;
}
else {
freq[arr[x] + 1 ]++;
}
}
int unique = 0 ;
for ( int x = 0 ; x <= n + 1 ; x++) {
if (freq[x] != 0 ) {
unique++;
}
}
return unique;
}
public static void main (String[] args)
{
int []arr = { 3 , 3 , 3 , 3 };
int n = 4 ;
int ans = uniqueNumbers(arr, n);
System.out.println(ans);
}
}
|
Python3
def uniqueNumbers(arr, n):
arr.sort()
freq = [ 0 ] * (n + 2 )
for val in arr:
if (freq[val - 1 ] = = 0 ):
freq[val - 1 ] + = 1
elif (freq[val] = = 0 ):
freq[val] + = 1
else :
freq[val + 1 ] + = 1
unique = 0
for val in freq:
if (val> 0 ):
unique + = 1
return unique
if __name__ = = "__main__" :
arr = [ 3 , 3 , 3 , 3 ]
n = 4
print (uniqueNumbers(arr, n))
|
C#
using System;
class GFG {
static int uniqueNumbers( int []arr, int n)
{
Array.Sort(arr);
int []freq = new int [n + 2];
for ( int i = 0; i < n + 2; i++)
freq[i] = 0;
for ( int x = 0; x < n; x++) {
if (freq[arr[x] - 1] == 0) {
freq[arr[x] - 1]++;
}
else if (freq[arr[x]] == 0) {
freq[arr[x]]++;
}
else {
freq[arr[x] + 1]++;
}
}
int unique = 0;
for ( int x = 0; x <= n + 1; x++) {
if (freq[x] != 0) {
unique++;
}
}
return unique;
}
public static void Main ( string [] args)
{
int []arr = { 3, 3, 3, 3 };
int n = 4;
int ans = uniqueNumbers(arr, n);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function uniqueNumbers(arr , n) {
arr.sort((a,b)=>a-b);
var freq = Array(n + 2).fill(0);
for (i = 0; i < n + 2; i++)
freq[i] = 0;
for (x = 0; x < n; x++) {
if (freq[arr[x] - 1] == 0) {
freq[arr[x] - 1]++;
}
else if (freq[arr[x]] == 0) {
freq[arr[x]]++;
}
else {
freq[arr[x] + 1]++;
}
}
var unique = 0;
for (x = 0; x <= n + 1; x++) {
if (freq[x] != 0) {
unique++;
}
}
return unique;
}
var arr = [ 3, 3, 3, 3 ];
var n = 4;
var ans = uniqueNumbers(arr, n);
document.write(ans);
</script>
|
Time Complexity Analysis:
- The time taken to sort the given array is O(N * log(N)) where N is the size of the array.
- The time taken to run a loop over the sorted array to perform the operations is O(N).
- The time taken to run a loop over the hash to count the unique values is O(N).
- Therefore, the overall time complexity is O(N * log(N)) + O(N) + O(N). Since N * log(N) is greater, the final time complexity of the above approach is O(N * log(N)).
Auxiliary Space: O(N)
- The extra space taken by the freq array is O(N). Therefore the auxiliary space is O(N).