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 1
- or decreasing the element by 1
- or keeping the element as it is.
Note: No element can be less than or equal to 0.
Examples:
Input: arr = [4, 4, 5, 5, 5, 5, 6, 6]
Output: 5
Explanation: After modification of each element of the array in any of the three possible ways, arr[] = [3, 4, 5, 5, 5, 5, 6, 7]. Here distinct elements are 5.
Input: arr = [1, 1, 1, 8, 8, 8, 9, 9]
Output: 6
Explanation: After modification of each element of the array in any of the three possible ways, arr[] = [1, 1, 2, 7, 8, 8, 9, 10]. Here distinct elements are 6.
Approach: The idea is to sort the given array first, so that the elements can be checked easily, if it is distinct, by comparing with adjacent elements.
- First, sort all elements of the array.
- Initialize variables count and prev to 0. (To store the count of distinct elements and previous element respectively.)
- After that keep a track of the previous element using prev variable.
- Iterate the sorted array.
- Decrease the current element’s value by 1 and check if the previous element is lesser than the decreased value. If it is lesser then increment the count and assign current value to prev.
- If the decreased value of the current element is not greater than the previous element then keep the current element as it is and check if the previous element is lesser than the current element. If it is lesser then increment the count and assign current value to prev.
- If the current value is not greater than the previous element then increment the current value by 1 and check if the previous element is lesser than the incremented current element. If it is lesser then increment the count and assign current value to prev.
- If incremented value of current element is not lesser than previous value then skip that element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int max_dist_ele( int arr[],
int n)
{
sort(arr, arr + n);
int ans = 0;
int prev = 0;
for ( int i = 0;
i < n; i++) {
if (prev < (arr[i] - 1)) {
ans++;
prev = arr[i] - 1;
}
else if (prev < (arr[i])) {
ans++;
prev = arr[i];
}
else if (prev < (arr[i] + 1)) {
ans++;
prev = arr[i] + 1;
}
}
return ans;
}
int main()
{
int arr[] = { 1, 1, 1, 8,
8, 8, 9, 9 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << max_dist_ele(arr, n)
<< endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int max_dist_ele(
int arr[], int n)
{
Arrays.sort(arr);
int ans = 0 ;
int prev = 0 ;
for ( int i = 0 ;
i < n; i++) {
if (prev < (arr[i] - 1 )) {
ans++;
prev = arr[i] - 1 ;
}
else if (prev < (arr[i])) {
ans++;
prev = arr[i];
}
else if (prev < (arr[i] + 1 )) {
ans++;
prev = arr[i] + 1 ;
}
}
return ans;
}
public static void main(String args[])
{
int arr[] = { 1 , 1 , 1 , 8 ,
8 , 8 , 9 , 9 };
int n = arr.length;
System.out.println(max_dist_ele(arr, n));
}
}
|
Python3
def max_dist_ele(arr, n):
arr.sort()
ans = 0
prev = 0
for i in range (n):
if prev < (arr[i] - 1 ):
ans + = 1 ;
prev = arr[i] - 1
elif prev < (arr[i]):
ans + = 1
prev = arr[i]
elif prev < (arr[i] + 1 ):
ans + = 1
prev = arr[i] + 1
return ans
arr = [ 1 , 1 , 1 , 8 , 8 , 8 , 9 , 9 ]
n = len (arr)
print (max_dist_ele(arr, n))
|
C#
using System;
class GFG{
static int max_dist_ele( int []arr, int n)
{
Array.Sort(arr);
int ans = 0;
int prev = 0;
for ( int i = 0; i < n; i++)
{
if (prev < (arr[i] - 1))
{
ans++;
prev = arr[i] - 1;
}
else if (prev < (arr[i]))
{
ans++;
prev = arr[i];
}
else if (prev < (arr[i] + 1))
{
ans++;
prev = arr[i] + 1;
}
}
return ans;
}
public static void Main(String []args)
{
int []arr = { 1, 1, 1, 8,
8, 8, 9, 9 };
int n = arr.Length;
Console.WriteLine(max_dist_ele(arr, n));
}
}
|
Javascript
<script>
function max_dist_ele( arr, n)
{
arr.sort();
var ans = 0;
var prev = 0;
for (let i = 0;
i < n; i++) {
if (prev < (arr[i] - 1)) {
ans++;
prev = arr[i] - 1;
}
else if (prev < (arr[i])) {
ans++;
prev = arr[i];
}
else if (prev < (arr[i] + 1)) {
ans++;
prev = arr[i] + 1;
}
}
return ans;
}
var arr = new Array( 1, 1, 1, 8,
8, 8, 9, 9 );
var n = arr.length;
document.write(max_dist_ele(arr, n));
</script>
|
Time Complexity: O(N*logN)
Auxiliary Space: O(1) as it is using constant space for variables
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!