## Minimum Split

### December 4, 2015

Before you look at the suggested solution, check what your solution does when given the list (5 -5).

Our algorithm considers each split from left to right, beginning with the split that has no items in the left split and the entire list in the right split. At each step, we move one item from right to left, recompute totals, and if the current difference is less than the current minimum, update the minimum. The algorithm stops when the list is exhausted or the difference drops to zero.

(define (min-split xs) (let loop ((i 0) (lo 0) (los (list)) (hi (sum xs)) (his xs) (best-point 0) (best-value (sum xs))) (cond ((or (null? his) (= lo hi)) (split best-point xs)) ((< (abs (- lo hi)) best-value) (loop (+ i 1) (+ lo (car his)) (cons (car his) los) (- hi (car his)) (cdr his) i (abs (- lo hi)))) (else (loop (+ i 1) (+ lo (car his)) (cons (car his) los) (- hi (car his)) (cdr his) best-point best-value)))))

Here are some examples:

> (min-split '(2 7 3 1 4)) (2 7) (3 1 4) > (min-split '(1 2 4 7 3)) (1 2 4) (7 3) > (min-split '(5 -5)) () (5 -5)

We used `sum`

and `split`

from the Standard Prelude. You can run the program at http://ideone.com/2GOsm9.

In Python

It’s not very clear whether this means the minimum difference, or the minimum absolute value of the difference. The wording of page 2 suggests the latter, but page 1 doesn’t say so.

Haskell:

Python:

def bestSplit(lst):

diff = 100000000

idx = 0

for count in range(0, len(lst)):

if diff > abs(sum(lst[:count], 0) – sum(lst[count:], count)):

diff = abs(sum(lst[:count], 0) – sum(lst[count:], count))

idx = count

return idx

Solution in Python

Third attempt in posting the code :p

In Python.

C++ attempt

#include “math.h”

#include

std::vector numberList;

numberList.push_back(3);

numberList.push_back(7);

numberList.push_back(4);

numberList.push_back(2);

numberList.push_back(1);

std::vector differences;

for(int split = 0; split < numberList.size(); ++split)

{

int leftSum = 0;

for(int left = 0; left < split; ++left) {

leftSum += numberList[left];

}

int rightSum = 0;

for(int right = split; right < numberList.size(); ++right) {

rightSum += numberList[right];

}

int difference = abs(rightSum – leftSum);

differences.push_back(difference);

}

int lowestDiffIndex = 0;

int lowestDiffValue = differences[0];

for(int i = 0; i < differences.size(); ++i)

{

lowestDiffIndex = (differences[i] < lowestDiffValue) ? i : lowestDiffIndex;

lowestDiffValue = (differences[i] < lowestDiffValue) ? differences[i] : lowestDiffValue;

}

// Split is at element lowestDiffIndex

A solution in Racket.