Arrays - Minimum Absolute Difference Between 2 Non Contiguous Equal Subarrays - Stack Overflow
Arrays - Minimum Absolute Difference Between 2 Non Contiguous Equal Subarrays - Stack Overflow
How are we doing? Please help us improve Stack Overflow. Take our short survey
Note: I know this question has been asked here, but I didn't properly understand the solution, and
I don't have enough points to comment to clarify my query. (I'm a beginner on SO)
3
Question: Given an array, of size n, divide the array into 2 non contiguous subarrays of size n/2 &
n/2 (if n is even), and (n-1)/2 & (n+1)/2, (if n is odd), such that the absolute difference between
the sums of the 2 subarrays is minimum.
2
Example:
4 5 3 55 -3 23 3 20 100 90
Answer: 0.
Explanation: {100, 20, 23, 4, 3} and {5, 55, -3, 3, 90} , both of which sum to
150.
I know greedy approach won't work for this, and feel that this involves dynamic programming.
However, I am unable to model a solution that ensures that the subarrays being considered are of
equal size, my solution generates minimum difference subarrays of arbitrary size, which is a
general partition problem.
I am also unsure whether making a table is a good idea, because the numbers can be huge.
(Although its certain that all input is within bounds for int).
Here is the link to the question that has already been asked (and answered): Dividing an array into
two subsets of equal sizes having minimum difference in sum of values.
Share Follow edited May 23 '17 at 12:22 asked Aug 5 '16 at 13:53
Community ♦ Utkarsh
1 1 81 1 4
2 private static long minimalDiff(int[] nums, int sumA, int sumB, int indexA, int
indexB) {
if (index == nums.length) {
return Integer.MAX_VALUE;
if (result > 0) {
return result;
} else {
if (result > 0) {
return result;
}
}
}
return minimalDiff(num, 0, 0, -1, -1);
System.out.println("Answer: "+minimalDiff(test));
It prints:
Answer: 0
1 Please don't just dump code as an answer, add an explanation of how the algorithm works.
– O.O.Balance
Jun 15 '18 at 18:17
Here is an c++ implementation for the problem using recursion.
1 #include<bits/stdc++.h>
void solve(int ele,int currSum,int index,int maxe,int * arr,int & answer,int sum,int
n){
if(ele==maxe){
int ssum=sum-currSum;
if(abs(currSum-ssum)<answer)
answer=abs(currSum-ssum);
return;
if(index>=n){
return;
solve(ele+1,currSum+arr[index],index+1,maxe,arr,answer,sum,n);
solve(ele,currSum,index+1,maxe,arr,answer,sum,n);
int sum=0;
for(int i=0;i<n;i++){
sum+=arr[i];
}
int answer=INT_MAX;
solve(0,0,0,n/2,arr,answer,sum,n);
return answer;
int main(){
int n,x;
cin>>n;
for(int i=0;i<n;i++){
cin>>x;
arr[i]=x;
}
cout<<FindMinimumDifference(arr,n);
return 0;
1 Please don't just dump code as an answer, add an explanation of how the algorithm works.
– O.O.Balance
Jun 15 '18 at 18:17
If one subset of half size is formed, the remaining elements form the other subset. We initialize
current set as empty and one by one build it. There are two possibilities for every element, either
0 it is part of current set, or it is part of the remaining elements (other subset). We consider both
possibilities for every element. When the size of current set becomes n/2, we check whether this
solutions is better than the best solution available so far. If it is, then we update the best solution.
Share Follow answered Jul 20 at 14:34
Manan Chhajer
1
1 Good roadmap. A simple code-snippet would have been very helpful for the community here.
– Jürgen Fink
Jul 20 at 18:46