Problem
You are given an integer array a of length n and a non-negative integer k.
For each index i, you must choose:
-
an integer offset
d_i
such that
0 <= d_i <= k
, and
-
a sign, either
+
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.
Example
-
Input:
a = [0, 0, 0]
,
k = 1
-
Output:
3
-
Explanation: Choose offsets
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.
Notes / Clarifications
-
Offsets are integers.
-
Offset
0
is allowed.
-
The sign choice is independent per element.
Constraints (typical interview setting)
-
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.