Skip to content

Instantly share code, notes, and snippets.

@the-fejw
Created February 3, 2014 10:00
Show Gist options
  • Save the-fejw/8781249 to your computer and use it in GitHub Desktop.
Save the-fejw/8781249 to your computer and use it in GitHub Desktop.
CLRS 4.1.2 Brute Force
public static int[] bruteForce(int[] A, int low, int high) {
int[] result = new int[3]; //result[0]: max-left, result[1]: max-right, result[2]: max-sum
int sum = 0;
int maxSum = Integer.MIN_VALUE;
for (int i = low; i <= high; i++) {
for (int j = i; j <= high; j++) {
sum += A[j];
if (sum > maxSum) {
maxSum = sum;
result[0] = i;
result[1] = j;
}
}
sum = 0;
}
result[2] = maxSum;
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment