Minimum Score by Changing Two Elements - Problem
Minimum Score by Changing Two Elements

You're given an integer array nums and need to minimize its "score" by strategically changing exactly two elements.

The score of an array is calculated as:
Low score: minimum absolute difference between any two integers
High score: maximum absolute difference between any two integers
Total score = low score + high score

Your goal is to change exactly two elements in the array to any values you want, then return the minimum possible score.

Example: For [1, 4, 3], the current score is 1 + 3 = 4. By changing two elements optimally, you can achieve a lower score.

Input & Output

example_1.py — Basic case
$ Input: nums = [1, 4, 3]
Output: 0
💡 Note: We can change the array to [4, 4, 3]. The low score is min(|4-4|, |4-3|, |4-3|) = 0. The high score is max(|4-4|, |4-3|, |4-3|) = 1. The score is 0 + 1 = 1. Actually, we can do better by changing to [3, 3, 3] to get score 0 + 0 = 0.
example_2.py — Larger array
$ Input: nums = [1, 4, 7, 8, 5]
Output: 3
💡 Note: We can change the array to [4, 4, 7, 8, 5]. The low score becomes 0 (from |4-4|) and the high score becomes 4 (from |8-4|). Total score is 0 + 4 = 4. We can do better by changing to [4, 4, 5, 5, 5] to get score 0 + 1 = 1. Even better: [4, 5, 5, 5, 5] gives us score 1 + 1 = 2. Best: [5, 5, 5, 5, 5] gives score 0 + 0 = 0, but we can only change 2 elements. Optimal is [1, 5, 5, 8, 5] with score 0 + 7 = 7. Actually optimal is [5, 4, 7, 5, 5] giving score 0 + 3 = 3.
example_3.py — Edge case
$ Input: nums = [2, 2]
Output: 0
💡 Note: With only 2 elements both the same, the low and high scores are both 0, so total score is 0. No changes needed, but we can change both to any value and still get score 0.

Constraints

  • 3 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 109
  • You must change exactly two elements

Visualization

Tap to expand
🎼 Orchestra Tuning StrategyStep 1: Analyze the DiscordOriginal notes: [1, 4, 7, 8, 5] → Sorted: [1, 4, 5, 7, 8]14578Range: 7, Min gap: 1Step 2: Strategy - Remove Extremes44577[4,4,5,7,7] → Score: 0+3=3Step 3: Strategy - Create Harmony55555[5,5,5,5,5] → Score: 0+0=0But we can only change 2!🎯 Optimal Strategy Found!Change positions strategically to minimize total discordBest approach: Systematic analysis of key strategies
Understanding the Visualization
1
Analyze Range
Sort the 'notes' to see the full range of discord
2
Identify Problems
Find the extreme notes causing the most discord
3
Strategic Tuning
Try different tuning strategies - remove extremes or create harmony
4
Minimize Discord
Choose the strategy that minimizes total discord (score)
Key Takeaway
🎯 Key Insight: The optimal solution requires analyzing multiple strategic approaches - removing extremes, creating equal elements, or balancing the range. Sorting first enables systematic evaluation of these strategies.
Asked in
Google 23 Amazon 18 Meta 15 Apple 12
34.4K Views
Medium-High Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen