Class Solution
java.lang.Object
g1601_1700.s1675_minimize_deviation_in_array.Solution
1675 - Minimize Deviation in Array.<p>Hard</p>
<p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>
<p>You can perform two types of operations on any element of the array any number of times:</p>
<ul>
<li>If the element is <strong>even</strong> , <strong>divide</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,2].</code></li>
</ul>
</li>
<li>If the element is <strong>odd</strong> , <strong>multiply</strong> it by <code>2</code>.
<ul>
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[2,2,3,4].</code></li>
</ul>
</li>
</ul>
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>
<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums = [1,2,3,4]</p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong> You can transform the array to [1,2,3,2], then to [2,2,3,2], then the deviation will be 3 - 2 = 1.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums = [4,1,5,20,3]</p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong> You can transform the array after two operations to [4,2,5,5,3], then the deviation will be 5 - 2 = 3.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> nums = [2,10,8]</p>
<p><strong>Output:</strong> 3</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
minimumDeviation
public int minimumDeviation(int[] nums)
-