0% found this document useful (0 votes)
73 views

Arrays - Minimum Absolute Difference Between 2 Non Contiguous Equal Subarrays - Stack Overflow

The document is a request from Stack Overflow to take a survey to help them improve the site. It provides a link to the survey and encourages users to provide feedback.

Uploaded by

lege
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Arrays - Minimum Absolute Difference Between 2 Non Contiguous Equal Subarrays - Stack Overflow

The document is a request from Stack Overflow to take a survey to help them improve the site. It provides a link to the survey and encourages users to provide feedback.

Uploaded by

lege
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

 

How are we doing? Please help us improve Stack Overflow. Take our short survey

Minimum absolute difference between 2 non contiguous equal


subarrays
Asked
5 years, 1 month ago Active
1 month ago Viewed
4k times

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).

Can anyone provide me an algorithm that I could apply?

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.

arrays algorithm dynamic-programming

Share Follow edited May 23 '17 at 12:22 asked Aug 5 '16 at 13:53
Community ♦ Utkarsh
1 1 81 1 4

Please provide a link to the original question.


– MrSmith42
Aug 5 '16 at 14:06

@MrSmith42 I have provided the link in the question now.


–  Utkarsh
Aug 5 '16 at 14:18
3 Answers Active Oldest Votes

A recursive implementation (Java):

2 private static long minimalDiff(int[] nums, int sumA, int sumB, int indexA, int
indexB) {

int index = indexA + indexB + 2;

if (index == nums.length) {

return Math.abs(sumA - sumB);

} else if (Math.max(indexA, indexB) * 2 > nums.length - 1) {

return Integer.MAX_VALUE;

} else if (Math.abs(sumA + nums[index] - sumB) < Math.abs(sumB + nums[index] -


sumA)){

long result = minimalDiff(nums, sumA + nums[index], sumB, indexA + 1, indexB);

if (result > 0) {

result = Math.min(result, minimalDiff(nums, sumB + nums[index], sumA,


indexB + 1, indexA));

return result;

} else {

long result = minimalDiff(nums, sumB + nums[index], sumA, indexB + 1, indexA);

if (result > 0) {

result = Math.min(result, minimalDiff(nums, sumA + nums[index], sumB,


indexA + 1, indexB));

return result;

}
}

public static long minimalDiff(int[] num) {

if (num == null || num.length < 2){

throw new IllegalArgumentException("num");

}
return minimalDiff(num, 0, 0, -1, -1);

public static void main(String[] args) {

int [] test= {4, 5, 3, 55, -3, 23, 3, 20, 100, 90};

System.out.println("Answer: "+minimalDiff(test));

It prints:

Answer: 0

Share Follow answered Aug 5 '16 at 16:54


David Pérez Cabrera
4,614 2 21 35

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>

using namespace std;

void solve(int ele,int currSum,int index,int maxe,int * arr,int & answer,int sum,int
n){

// cout<<index<<" "<<ele<<" "<<currSum;

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 FindMinimumDifference(int *arr,int 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;

int * arr=new int[n];

for(int i=0;i<n;i++){

cin>>x;

arr[i]=x;

}
cout<<FindMinimumDifference(arr,n);

return 0;

Share Follow answered Jun 15 '18 at 17:47


user8902733
11 2

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

1 Please have a look at Why should I not #include <bits/stdc++.h>?


– Adrian W
Jun 15 '18 at 18:18

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

You might also like