Similar Problems

Similar Problems not available

Split With Minimum Sum - Leetcode Solution

Companies:

LeetCode:  Split With Minimum Sum Leetcode Solution

Difficulty: Easy

Topics: greedy math sorting  

Problem statement:

Given a list of integers nums, split it into two lists of non-zero integers such that the absolute difference between the sum of the two lists is minimized and return the minimum absolute difference.

Example:

Input: nums = [1,2,3,4,5] Output: 1 Explanation: We can split the list in two [1, 4] and [2, 3], with a difference of 1 between the sums.

Solution:

To solve the Split With Minimum Sum problem, we can use dynamic programming (DP) approach. We can start by finding the total sum of the given input list. Then, we can create a DP table with dimensions n+1 rows and (sum/2)+1 columns. DP[i][j] represents if we can form a subset with the first i numbers of the given list such that it adds up to j.

We can fill the DP table using the following conditions:

  • If the sum is even, then the minimum absolute difference between the two lists will be zero. So, we can return 0 as the answer.
  • If the sum is odd, then there can't be two lists with equal sums. In such cases, we can return 1 as the answer because the minimum absolute difference can't be less than one.
  • If the element is zero, then we can't use it to form a non-zero subset. So, we can mark the DP[0][j] as false for all values of j (except the first one which is DP[0][0] = true).
  • If the value of j is less than the value of nums[i-1], then we can't use nums[i-1] in the subset, so we can simply use DP[i-1][j] value for this case.
  • If the value of j is greater than or equal to the value of nums[i-1], then we can either include this i-th element in the subset or exclude it and take the value from DP[i-1][j]. So, in this case DP[i][j] will be DP[i-1][j] OR DP[i-1][j-nums[i-1]].

Finally, we can iterate through the last row of the DP table column-wise, starting from the (sum/2) column towards the first column. The first column with a True value will represent the non-zero subset that has the minimum absolute difference between its sum and the other subset's sum. We can return the difference between the total sum and twice this first column as the answer.

Here is the Python code implementation of the above DP-based approach:

def splitArray(nums): n = len(nums) total_sum = sum(nums) if total_sum % 2 != 0: # odd sum case return 1 half_sum = total_sum // 2 dp = [[False](half_sum+1) for _ in range(n+1)] for i in range(n+1): dp[i][0] = True for i in range(1, n+1): for j in range(1, half_sum+1): if nums[i-1] == 0: dp[i][j] = dp[i-1][j] elif j < nums[i-1]: dp[i][j] = dp[i-1][j] else: dp[i][j] = dp[i-1][j] or dp[i-1][j-nums[i-1]] for j in range(half_sum, -1, -1): if dp[n][j]: return total_sum - 2j

Time Complexity: O(n*sum/2), where n is the length of the input list and sum is the total sum of all elements in the list.

Space Complexity: O(n*sum/2), as we are using a 2D DP table of this size.

Split With Minimum Sum Solution Code

1