2Brute2Best
🔥0

Minimize Deviation in Array

Hard·LeetCode #1675 Pro OnlyPattern Transfer
LeetCode
01

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.

Input

An array nums of positive integers.

Output

One integer: the smallest deviation you can reach after any number of operations.

Example 1
Input: nums = [1,2,3,4]
Output: 1

Double the 1 to get 2, and halve the 4 to get 2. The array becomes [2,2,3,2], and 3 − 2 = 1.

Example 2
Input: nums = [4,1,5,20,3]
Output: 3

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.

Example 3
Input: nums = [2,10,8]
Output: 3

Halve 10 to 5 and 8 to 4. The array becomes [2,5,4], and 5 − 2 = 3.

Constraints
  • • 2 ≤ nums.length ≤ 5 · 10^4
  • • 1 ≤ nums[i] ≤ 10^9
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“even: halve, odd: double”
→ Each number follows one chain of values. Doubling odd numbers once puts each chain at its top, so later moves only go down.
“deviation = largest − smallest”
→ The gap has two ends, but only the largest end can move in a useful way.
“smallest possible deviation”
→ Keep track of the best gap seen during the process, not only the final one.
03

Approach Complexity Ladder

Brute Force — try every reachable value of every numberO(K^N)O(N · K)

List the values each number can reach, try every combination, and keep the smallest max − min

Optimal — double the odd numbers, then halve the largest in a max-heapO(N log N · log M)O(N)

Double odd numbers first; in a max-heap, repeatedly halve the largest while it is even, and track max − min

Brute Force Intuition

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:

The best choice changes as you go — keep candidates in a heap and always pop the current best.
Remember this · Greedy
Trigger
Maximise count / minimise cost with a natural "best next step", sorting makes the choice obvious, reachability.
Think
Is there a choice I can always make safely without looking ahead?
Key question
Why can't the greedy choice be worse than the optimal one? (Try to break it with a small example.)
Brute2Best Pro Content

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.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Greedy translate into production architectures and low-level memory mechanics.