Skip to main content
Back to problems
#2740
Medium Algorithms

Find the value of the partition

Array Sorting
64.8% acceptance
Feb 25, 2026
324
25
You are given a positive integer array nums. Partition nums into two arrays, nums1 and nums2, such that: Each element of the array nums belongs to either the array nums1 or the array nums2. Both arrays are non-empty. The value of the partition is minimized. The value of the partition is |max(nums1) - min(nums2)|. Return the integer denoting the value of such partition.

Solution

Rust
Time O(n log n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn find_value_of_partition(nums: Vec<i32>) -> i32 {
    let mut nums = nums;
    nums.sort();
    nums.windows(2).map(|w| w[1] - w[0]).min().unwrap()
  }
}