Leetcode Problem 1509. Minimum Difference Between Largest and Smallest Value in Three Moves

1509. Minimum Difference Between Largest and Smallest Value in Three Moves

Leetcode Solutions

Sort and Compare Extremes

  1. If the length of the array is less than or equal to 4, return 0 because we can make all elements equal.
  2. Sort the array in ascending order.
  3. Calculate the difference between the fourth largest element and the smallest element.
  4. Calculate the difference between the largest element and the fourth smallest element.
  5. Calculate the difference between the third largest element and the second smallest element.
  6. Calculate the difference between the second largest element and the third smallest element.
  7. Return the minimum of the four calculated differences.
UML Thumbnail

Partial Sorting and Heap

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...