Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Maximum Number of Distinct Elements After Operations

XPChallenge Points: 20
levelLevel: Medium

You are given an array nums and an integer k.
For every element in the array, you may change it at most once by adding any integer value between -k and k (inclusive).
Your goal is to maximize the number of unique (distinct) values in the array after optionally modifying the elements.

Return the maximum possible count of distinct numbers you can achieve.

Example 1:

Input: nums = [5,5,6,6,6], k = 1

Output: 4

Explanation:

Modify values to: [4, 5, 6, 7, 8] Now there are 4 distinct values → {4,5,6,7,8}

Example 2:

Input: nums = [10,10,10], k = 2

Output: 3

Explanation:

Changes could be: [8,10,12] We get 3 different numbers.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result