
You are given an integer array a of length n and a non-negative integer k.
For each index i, you must choose:
d_i
such that
0 <= d_i <= k
, and
+
or
-
,
and replace a[i] with a[i] + d_i or a[i] - d_i.
Constraint: All chosen offsets must be distinct across indices (i.e., d_i != d_j for i != j).
Your goal is to maximize the number of distinct integers in the resulting array after all replacements.
Return that maximum possible number of distinct values.
a = [0, 0, 0]
,
k = 1
3
0, 1
(but offsets must be distinct, so for 3 elements we can use
d = 0, 1
only if
k=1
; instead, one valid construction is to allow
d
to be chosen per element distinctly within
[0,k]
—for this example, a maximal distinct outcome is
[-1, 0, 1]
by applying
-1, 0, +1
to the three zeros.
0
is allowed.
1 <= n <= 2 * 10^5
0 <= k <= 10^9
-10^9 <= a[i] <= 10^9
Implement a function that returns the maximum possible number of distinct values.