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.
Input: arr = [4, 4, 5, 5, 5, 5, 6, 6]
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]
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:
Time Complexity: O(N*logN)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximize count of distinct elements possible in an Array from the given operation
- Third largest element in an array of distinct elements
- Minimize swaps required to maximize the count of elements replacing a greater element in an Array
- Construct a distinct elements array with given size, sum and element upper bound
- Form an array of distinct elements with each element as sum of an element from each array
- Count distinct elements after adding each element of First Array with Second Array
- Maximize the number of indices such that element is greater than element to its left
- Count of distinct possible pairs such that the element from A is greater than the element from B
- Maximize sum of atmost K elements in array by taking only corner elements | Set 2
- Maximize sum of K elements in Array by taking only corner elements
- Maximize the sum of differences of consecutive elements after removing exactly K elements
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array
- Length of array pair formed where one contains all distinct elements and other all same elements
- Split an array containing N elements into K sets of distinct elements
- Maximize the last Array element as per the given conditions
- Maximize the sum of modulus with every Array element
- Remove an element to maximize the GCD of the given array
- Remove array end element to maximize the sum of product
- Maximize array sum by X increments when each element is divided by 10
- Maximum distinct elements after removing k elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.