Class Solution
-
- All Implemented Interfaces:
public final class Solution3397 - Maximum Number of Distinct Elements After Operations.
Medium
You are given an integer array
numsand an integerk.You are allowed to perform the following operation on each element of the array at most once:
Add an integer in the range
[-k, k]to the element.
Return the maximum possible number of distinct elements in
numsafter performing the operations.Example 1:
Input: nums = 1,2,2,3,3,4, k = 2
Output: 6
Explanation:
numschanges to[-1, 0, 1, 2, 3, 4]after performing operations on the first four elements.Example 2:
Input: nums = 4,4,4,4, k = 1
Output: 3
Explanation:
By adding -1 to
nums[0]and 1 tonums[1],numschanges to[3, 5, 4, 4].Constraints:
<code>1 <= nums.length <= 10<sup>5</sup></code>
<code>1 <= numsi<= 10<sup>9</sup></code>
<code>0 <= k <= 10<sup>9</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegermaxDistinctElements(IntArray nums, Integer k)-
-
Method Detail
-
maxDistinctElements
final Integer maxDistinctElements(IntArray nums, Integer k)
-
-
-
-