Minimum Difference Subarrays in JavaAn input array inputArr[] is given to us that contains n numbers. Our task is to find the minimum difference between the two subarrays. The subarrays are made from the given input array. If an element is a part of one subarray, then it can not be a part of the other subarray. Also, none of the subarrays should be empty. The following examples show the same. Example 1: Input int arr[] = {7, 6, 8, 1, 5} Output: 1 Explanation: If we create two subarrays as: subArr1 = {6, 8} and subArr2 = {7, 1, 5}, then we get the sum of elements of subarrays as: 6 + 8 = 14, and 7 + 1 + 5 = 13, and their difference is 14  13 = 1. Hence, the output is 1. Example 2: Input int arr[] = {2, 4, 8, 9, 5, 7, 3, 6, 1} Output: 1 Explanation: If we create two subarrays as: subArr1 = {8, 9, 5} and subArr2 = {2, 4, 7, 3, 6, 1}, then we get the sum of elements of subarrays as: 8 + 9 + 5 = 22, and 2 + 4 + 7 + 3 + 6 + 1 = 23, and their difference is 23  22 = 1. Hence, the output is 1. Example 3: Input int arr[] = {3, 3, 3, 3, 3} Output: 3 Explanation: If we create two subarrays as: subArr1 = {3, 3, 3} and subArr2 = {3, 3}, then we get the sum of elements of subarrays as: 3 + 3 + 3 = 9, and 3 + 3 = 6, and their difference is 9  6 = 3. Hence, the output is 3. Approach: Brute ForceWe will find all the possible values of the first subarray, and for each possible value, we will find the value for the second subarray. Then, we will compute the absolute difference between the value of the first subarray and the second subarray, and the absolute minimum difference is our answer. The following program illustrates the same. FileName: MinDiffSubArrays.java Output: For the input array: 7 6 8 1 5 The minimum difference between the two subarrays is: 1 For the input array: 2 4 8 9 5 7 3 6 1 The minimum difference between the two subarrays is: 1 For the input array: 3 3 3 3 3 The minimum difference between the two subarrays is: 3 Complexity Analysis: Every recursive call creates two exponential calls. Hence, the time complexity of the program is exponential. The time complexity of the program is too high and is not suitable for the larger inputs. Therefore, we must do some optimization, and the same is mentioned in the following approach. Approach: Using PrefixSumWith the help of an auxiliary array (prefixArr[]), we will store the prefix sum of the input array (inputArr[]), where prefixArr[j], represents the sum of elements from 0 to j. Algorithm:Step 1: Declare the array prefixArr for storing the prefix sum of the input array. Step 2: Copy all the elements of the input array in the prefixArr. Step 3: For every index j from 1 to s  1 (s is the size of the input array), do the following:
Step 4: Declare a variable for storing the minimum difference, which is minDifference in our case. Step 5: Declare a variable for storing the sum of elements of the first subarray, which is firstSubArrSum in our case. Step 6: Declare a variable for storing the sum of elements of the second subarray, which is secondSubArrSum in our case. Step 7: Assign the maximum integer value to the variable minDifference. Step 8: For index j in the range 0 to s  2, do the following:
Step 9: Return the value of minDifference. ImplementationObserve the implementation of the abovementioned algorithm. FileName: MinDiffSubArrays.java Output: For the input array: 7 6 8 1 5 The minimum difference between the two subarrays is: 1 For the input array: 2 4 8 9 5 7 3 6 1 The minimum difference between the two subarrays is: 1 For the input array: 3 3 3 3 3 The minimum difference between the two subarrays is: 3 Complexity Analysis: There are three forloops that are used in the method findMinDiff(). However, none of them is nested. Thus, making the timecomplexity O(n). Also, an auxiliary array is getting used in the program (prefixArr[]), thus making the space complexity O(n), where n is the total number of elements present in the input array. We will be doing optimization on the space complexity. Observe the approach mentioned below. Approach: Using Constant SpaceIn this approach, we will be using variables instead of the auxiliary array. We will be using a variable totalEleSum for storing the sum of all of the elements of the input array. Another variable, firstSubArrSum, is also used for storing the sum of the elements of the first subarray. The sum of elements of the second subarray can be computed easily by totalEleSum  firstSubArrSum. Now, the comparison is made for computing the absolute minimum difference for each of the indexes. Observe the following program. FileName: MinDiffSubArrays.java Output: For the input array: 7 6 8 1 5 The minimum difference between the two subarrays is: 1 For the input array: 2 4 8 9 5 7 3 6 1 The minimum difference between the two subarrays is: 1 For the input array: 3 3 3 3 3 The minimum difference between the two subarrays is: 3 Complexity Analysis: The time complexity of the program is the same as the previous program. The space complexity of the program is constant, i.e., O(1), as the program is not using the auxiliary array.
Next TopicPlus One to Array Problem in Java
