Minimize Deviation in Array - Complete Solution Guide
Minimize Deviation in Array is LeetCode problem 1675, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 . For example, if the array is [1,2,3,4] , then you can do this operation on the last element, and the array will be [1,2,3, 2 ]. If the element is odd , multiply it by 2 . For example, if the array is [1,2,3,4] , then you can do this operation on the first element, and the array will be [ 2 ,2,3,4]. The deviation of
Detailed Explanation
The problem asks us to minimize the 'deviation' of an array of positive integers. Deviation is defined as the maximum difference between any two elements in the array. We are allowed to perform two types of operations any number of times: divide an even number by 2, or multiply an odd number by 2. The goal is to find the smallest possible deviation we can achieve after applying these operations.
Solution Approach
The solution uses a priority queue (max heap) to efficiently track the current maximum element and allows us to repeatedly reduce the largest element by dividing it by 2 until it becomes odd or reaches its minimum possible value (which is either the original value if it started even, or double the value if it started odd). We also maintain a running minimum value, as dividing the maximum may result in a new, smaller minimum. The difference between the current maximum and current minimum represents the deviation at any given moment, and we want to minimize this.
Step-by-Step Algorithm
- Step 1: Transform all odd numbers in the array by multiplying them by 2. This makes them even and ensures we only perform division operations going forward.
- Step 2: Insert all numbers into a max priority queue. Use a negative representation of the numbers so a min-heap is used as a max-heap.
- Step 3: Find the initial minimum value among all the numbers after transformation.
- Step 4: Initialize the minimum deviation as the difference between the current maximum (top of the heap) and the minimum value.
- Step 5: While the current maximum element (top of the heap) is even:
- a. Remove the current maximum from the heap.
- b. Divide the current maximum by 2.
- c. Insert the new value back into the heap.
- d. Update the minimum value if the new value is smaller than the current minimum.
- e. Update the minimum deviation if the current deviation is smaller than the previous minimum deviation.
- Step 6: Return the minimum deviation found.
Key Insights
- Insight 1: To minimize the deviation, we want to bring the maximum and minimum elements of the array as close as possible.
- Insight 2: Each odd number can only be multiplied by 2 once. Each even number can be divided by 2 multiple times until it becomes odd or 1.
- Insight 3: We can maximize each number initially (multiply odd numbers by 2). This ensures that we are only ever decreasing numbers (dividing by 2). The minimum element needs to be tracked separately.
Complexity Analysis
Time Complexity: O(n log n log M)
Space Complexity: O(n)
Topics
This problem involves: Array, Greedy, Heap (Priority Queue), Ordered Set.
Companies
Asked at: Samsung.