Introduction
LeetCode 462 is a classic mathematical and greedy problem.
We are given an integer array where each operation allows us to:
- Increment an element by 1
- Decrement an element by 1
Our task is to make all numbers equal while using the minimum number of moves.
At first glance, this may look like a simple array problem.
But the hidden concept behind this question is:
- Median property
- Greedy optimization
- Absolute difference minimization
This problem is extremely popular in coding interviews because it tests logical thinking more than coding complexity.
# Problem Link
Problem Statement
You are given an integer array nums.
In one move:
- You can increase an element by 1
- Or decrease an element by 1
You must make all array elements equal.
Return the minimum number of operations required.
Example 1
Explanation:
Total operations = 2
Example 2
Key Observation
We need to choose one target value such that all numbers move toward it.
Question:
Which target gives minimum total moves?
Answer:
Median
Median minimizes the sum of absolute differences.
Why Median Works?
Suppose:
If target = 2
If target = 5
Median gives minimum moves.
Approach 1: Brute Force
In this approach, we try every possible value as target.
For each target:
- Calculate total operations needed
- Store minimum answer
Algorithm
- Find minimum and maximum element
- Try every value between them
- Compute total moves
- Return minimum
Java Code (Brute Force)
Time Complexity
Very slow for large values.
Approach 2: Sorting + Median (Optimal)
This is the best and most commonly used approach.
Idea
- Sort array
- Pick median element
- Calculate total absolute difference
Steps
Step 1: Sort Array
Sorting helps us easily find median.
Step 2: Pick Median
Step 3: Calculate Moves
For every element:
Optimal Java Solution
Code Explanation
Step 1: Sort Array
Sorting allows median calculation.
Step 2: Get Median
Middle element becomes target.
Step 3: Compute Difference
Find distance from median.
Step 4: Add All Moves
Store total moves.
Approach 3: Two Pointer Optimization
After sorting, we can use two pointers.
Instead of calculating absolute difference manually, we can pair smallest and largest values.
Idea
After sorting:
Because both numbers will meet toward median.
Java Code (Two Pointer)
Why Two Pointer Works?
Because:
Pairing smallest and largest values gives direct movement cost.
Dry Run
Input:
Sort:
Median:
Operations:
Total:
Time Complexity
Sorting
Traversing
Total
Space Complexity
Ignoring sorting stack.
Common Mistakes
1. Using Average Instead of Median
Many people think average gives minimum.
Wrong.
Average minimizes squared difference.
Median minimizes absolute difference.
2. Forgetting Sorting
Median requires sorted order.
3. Overflow Issue
Values can be large.
Sometimes use:
for safer calculation.
4. Using Wrong Median Index
Correct:
Edge Cases
Case 1
Single element array.
Case 2
All elements already equal.
Case 3
Negative numbers.
Algorithm still works.
FAQs
Q1. Why median gives minimum moves?
Median minimizes total absolute difference.
Q2. Can average work?
No.
Average does not minimize absolute distance.
Q3. Is sorting necessary?
Yes.
Sorting helps us easily find median.
Q4. Which approach is best?
Sorting + median approach.
Interview Insight
Interviewers ask this problem to test:
- Median property understanding
- Greedy optimization
- Mathematical thinking
- Array manipulation
Conclusion
LeetCode 462 is one of the most important median-based interview questions.
The major learning is:
- Median minimizes total absolute difference
- Sorting makes finding median easy
- Sum of distances gives answer
Once you understand why median works, this question becomes very simple.




