Minimize Deviation in Array
Problem Statement
You get an array nums of positive integers. You can repeat these operations on any element as often as you like:
- If the element is even, halve it.
- If the element is odd, double it.
The deviation of the array is the largest value minus the smallest value. Return the smallest deviation you can reach.
An array nums of positive integers.
One integer: the smallest deviation you can reach after any number of operations.
Double the 1 to get 2, and halve the 4 to get 2. The array becomes [2,2,3,2], and 3 − 2 = 1.
Double the 1 to get 2, and halve the 20 twice to get 5. The array becomes [4,2,5,5,3], and 5 − 2 = 3.
Halve 10 to 5 and 8 to 4. The array becomes [2,5,4], and 5 − 2 = 3.
- • 2 ≤ nums.length ≤ 5 · 10^4
- • 1 ≤ nums[i] ≤ 10^9
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
List the values each number can reach, try every combination, and keep the smallest max − min
Double odd numbers first; in a max-heap, repeatedly halve the largest while it is even, and track max − min
For each number, find every value it can reach by repeating its one allowed operation. Then try every combination, one value per number, and keep the smallest max − min over all of them.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Greedy translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.