Shrink the Reachable Value Range. For positive integer array nums, you may repeatedly halve an even entry or double an odd entry. The deviation is the largest value minus the smallest.
### Problem
For positive integer array `nums`, you may repeatedly halve an even entry or double an odd entry. The deviation is the largest value minus the smallest. Return the least deviation reachable with any number of these operations.
```cpp
class Solution {
public:
int minimumDeviation(vector<int>& nums);
};
```
### Examples
**Example 1:**
```text
nums = [3,6,12]
Output: 0
Explanation: Halve 6 once and 12 twice to obtain [3,3,3].
```
**Example 2:**
```text
nums = [5,8]
Output: 1
Explanation: Halve 8 to 4, giving a difference of 1. The reachable values are {5,10} and {1,2,4,8}, so zero is impossible.
```
### Constraints
- `n == nums.length`, with `2 <= n <= 5*10^4`.
- `1 <= nums[i] <= 10^9`.
### Clarifying Questions
An odd value can only double to an even value, whose next halving returns to that odd value. Operations do not generate arbitrarily large powers of two.
```hint Inspect the structure
Which operation is legal at the current value, and when has a claimed target configuration actually been reached?
```
### Reasoning checkpoints
- Characterize which values are reachable under the parity-dependent operations.
- Show that the claimed deviation is attainable and no other reachable configuration is narrower.
### Optional extensions
What dual strategy starts from each entry’s minimum reachable value?
Overview: Shrink the Reachable Value Range. For positive integer array nums, you may repeatedly halve an even entry or double an odd entry. The deviation is the largest value minus the smallest.
For positive integer array nums, you may repeatedly halve an even entry or double an odd entry. The deviation is the largest value minus the smallest. Return the least deviation reachable with any number of these operations.
class Solution {
public:
int minimumDeviation(vector<int>& nums);
};
Examples
Example 1:
nums = [3,6,12]
Output: 0
Explanation: Halve 6 once and 12 twice to obtain [3,3,3].
Example 2:
nums = [5,8]
Output: 1
Explanation: Halve 8 to 4, giving a difference of 1. The reachable values are {5,10} and {1,2,4,8}, so zero is impossible.
Constraints
n == nums.length
, with
2 <= n <= 5*10^4
.
1 <= nums[i] <= 10^9
.
Clarifying Questions Guidance
An odd value can only double to an even value, whose next halving returns to that odd value. Operations do not generate arbitrarily large powers of two.
Reasoning checkpoints
Characterize which values are reachable under the parity-dependent operations.
Show that the claimed deviation is attainable and no other reachable configuration is narrower.
Optional extensions
What dual strategy starts from each entry’s minimum reachable value?