2740. Find the Value of the Partition LeetCode Solution

In this guide, you will get 2740. Find the Value of the Partition LeetCode Solution with the best time and space complexity. The solution to Find the Value of the Partition problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find the Value of the Partition solution in C++
  4. Find the Value of the Partition solution in Java
  5. Find the Value of the Partition solution in Python
  6. Additional Resources
2740. Find the Value of the Partition LeetCode Solution image

Problem Statement of Find the Value of the Partition

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)|.
Here, max(nums1) denotes the maximum element of the array nums1, and min(nums2) denotes the minimum element of the array nums2.
Return the integer denoting the value of such partition.

Example 1:

Input: nums = [1,3,2,4]
Output: 1
Explanation: We can partition the array nums into nums1 = [1,2] and nums2 = [3,4].
– The maximum element of the array nums1 is equal to 2.
– The minimum element of the array nums2 is equal to 3.
The value of the partition is |2 – 3| = 1.
It can be proven that 1 is the minimum value out of all partitions.

See also  1253. Reconstruct a 2-Row Binary Matrix LeetCode Solution

Example 2:

Input: nums = [100,1,10]
Output: 9
Explanation: We can partition the array nums into nums1 = [10] and nums2 = [100,1].
– The maximum element of the array nums1 is equal to 10.
– The minimum element of the array nums2 is equal to 1.
The value of the partition is |10 – 1| = 9.
It can be proven that 9 is the minimum value out of all partitions.

Constraints:

2 <= nums.length <= 105
1 <= nums[i] <= 109

Complexity Analysis

  • Time Complexity: O(\texttt{sort})
  • Space Complexity: O(\texttt{sort})

2740. Find the Value of the Partition LeetCode Solution in C++

class Solution {
 public:
  int findValueOfPartition(vector<int>& nums) {
    int ans = INT_MAX;

    sort(begin(nums), end(nums));

    for (int i = 1; i < nums.size(); ++i)
      ans = min(ans, nums[i] - nums[i - 1]);

    return ans;
  }
};
/* code provided by PROGIEZ */

2740. Find the Value of the Partition LeetCode Solution in Java

class Solution {
  public int findValueOfPartition(int[] nums) {
    int ans = Integer.MAX_VALUE;

    Arrays.sort(nums);

    for (int i = 1; i < nums.length; ++i)
      ans = Math.min(ans, nums[i] - nums[i - 1]);

    return ans;
  }
}
// code provided by PROGIEZ

2740. Find the Value of the Partition LeetCode Solution in Python

class Solution:
  def findValueOfPartition(self, nums: list[int]) -> int:
    return min(b - a for a, b in itertools.pairwise(sorted(nums)))
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.