/** * Implements a brute force solution to the subset sum problem * @author alchambers */ public class BruteForce implements SubsetIfc { @Override public SubsetInfo solve(int[] array) { SubsetInfo solt = new SubsetInfo(); int best = Integer.MIN_VALUE; // Consider all starting positions of the subset for(int start = 0; start < array.length; start++){ int sum = 0; // Consider all possible end positions for the subset for(int i = start; i < array.length; i++){ sum += array[i]; // We found a better sum if(sum > best){ best = sum; solt.start = start; solt.end = i; solt.sum = sum; } } } return solt; } }