Minimize Deviation in Array - Problem
You are given an array nums of n positive integers.
You can perform two types of operations on any element of the array any number of times:
- If the element is even, divide it by 2.
- If the element is odd, multiply it by 2.
The deviation of the array is the maximum difference between any two elements in the array.
Return the minimum deviation the array can have after performing some number of operations.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,2,3,4]
›
Output:
1
💡 Note:
Convert 1→2, 3→6. Array becomes [2,2,6,4]. Then reduce 6→3, 4→2. Final: [2,2,3,2]. Deviation = 3-2 = 1.
Example 2 — All Odds
$
Input:
nums = [4,1,5,20,3]
›
Output:
3
💡 Note:
Convert odds: 1→2, 5→10, 3→6. Array: [4,2,10,20,6]. After optimal reductions: deviation is 3.
Example 3 — Single Element
$
Input:
nums = [2]
›
Output:
0
💡 Note:
Only one element, so deviation is 0.
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ nums[i] ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
[1,2,3,4] with operations: odd×2, even÷2
2
Transform Strategy
Convert odds to evens, then reduce largest elements
3
Optimal Result
Minimum possible deviation = 1
Key Takeaway
🎯 Key Insight: Convert all odds to evens first, then greedily reduce the maximum element until optimal
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code