|
| 1 | +/** |
| 2 | + * Link: https://leetcode.com/problems/split-array-with-minimum-difference/ |
| 3 | + * Title: Split Array With Minimum Difference |
| 4 | + * |
| 5 | + * Question: |
| 6 | + * You are given a 0-indexed integer array nums of length n. |
| 7 | + * Split the array into two non-empty parts such that: |
| 8 | + * - The left part contains the first i + 1 elements. |
| 9 | + * - The right part contains the remaining n - i - 1 elements. |
| 10 | + * |
| 11 | + * The difference between the two parts is the absolute difference |
| 12 | + * between the sum of the left part and the sum of the right part. |
| 13 | + * |
| 14 | + * Return the minimum difference possible between the two parts. |
| 15 | + * |
| 16 | + * -------------------------------------------------------------------- |
| 17 | + * Approach: |
| 18 | + * - Precompute prefix sums (left sums) and track if the left part is strictly increasing. |
| 19 | + * - Precompute suffix sums (right sums) and track if the right part is strictly decreasing. |
| 20 | + * - For each possible split index i: |
| 21 | + * - If left[0..i] is strictly increasing and right[i+1..n-1] is strictly decreasing, |
| 22 | + * calculate the difference = |leftSum - rightSum|. |
| 23 | + * - Keep track of the minimum difference found. |
| 24 | + * - If no valid split exists, return -1. |
| 25 | + * |
| 26 | + * -------------------------------------------------------------------- |
| 27 | + * Dry Run: |
| 28 | + * nums = [2, 3, 1] |
| 29 | + * n = 3 |
| 30 | + * left: |
| 31 | + * leftSum = [2, 5, 6] |
| 32 | + * leftValid = [true, true, false] (since 3 > 1 fails) |
| 33 | + * right: |
| 34 | + * rightSum = [6, 4, 1] |
| 35 | + * rightValid = [false, true, true] |
| 36 | + * |
| 37 | + * Possible splits: |
| 38 | + * i = 0 → leftValid[0] && rightValid[1] = true && true |
| 39 | + * diff = |2 - 4| = 2 |
| 40 | + * i = 1 → leftValid[1] && rightValid[2] = true && true |
| 41 | + * diff = |5 - 1| = 4 |
| 42 | + * Answer = min(2, 4) = 2 |
| 43 | + * |
| 44 | + * -------------------------------------------------------------------- |
| 45 | + * Time Complexity: O(n) |
| 46 | + * - One pass for prefix sums and checks, one pass for suffix sums and checks, |
| 47 | + * and one pass for evaluating splits. |
| 48 | + * Space Complexity: O(n) |
| 49 | + * - Arrays leftSum, rightSum, leftValid, rightValid each take O(n). |
| 50 | + */ |
| 51 | + |
| 52 | +class Solution { |
| 53 | + public long splitArray(int[] nums) { |
| 54 | + int n = nums.length; |
| 55 | + |
| 56 | + // prefix sums + strictly increasing check |
| 57 | + boolean[] left = new boolean[n]; |
| 58 | + long[] leftSum = new long[n]; |
| 59 | + left[0] = true; |
| 60 | + leftSum[0] = nums[0]; |
| 61 | + for (int i = 1; i < n; i++) { |
| 62 | + left[i] = left[i - 1] && nums[i - 1] < nums[i]; |
| 63 | + leftSum[i] = leftSum[i - 1] + nums[i]; |
| 64 | + } |
| 65 | + |
| 66 | + // suffix sums + strictly decreasing check |
| 67 | + boolean[] right = new boolean[n]; |
| 68 | + long[] rightSum = new long[n]; |
| 69 | + right[n - 1] = true; |
| 70 | + rightSum[n - 1] = nums[n - 1]; |
| 71 | + for (int i = n - 2; i >= 0; i--) { |
| 72 | + right[i] = right[i + 1] && nums[i] > nums[i + 1]; |
| 73 | + rightSum[i] = rightSum[i + 1] + nums[i]; |
| 74 | + } |
| 75 | + |
| 76 | + // evaluate all splits |
| 77 | + long min = Long.MAX_VALUE; |
| 78 | + boolean found = false; |
| 79 | + for (int i = 0; i < n - 1; i++) { |
| 80 | + if (left[i] && right[i + 1]) { |
| 81 | + min = Math.min(min, Math.abs(leftSum[i] - rightSum[i + 1])); |
| 82 | + found = true; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return found ? min : -1; |
| 87 | + } |
| 88 | +} |
0 commit comments