Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // See http://codereview.stackexchange.com/questions/52270/finding-the-sub-array-with-the-maximum-sum-my-approach
- public class MyApproach {
- public static void main(String[] args) {
- assertArrayScan(5, 11,new int[]{4, -5, -1, 0, -2, 20, -4, -3, -2, 8, -1, 10, -1, -1 });
- assertArrayScan(3, 8, new int[]{-5, 1, -3, 7, -1, 2, 1, -4, 6});
- assertArrayScan(3, 6, new int[]{-5, 1, -3, 7, -1, 2, 1, -6, 5});
- assertArrayScan(1, 4, new int[]{-5, 6, -3, -2, 7, -5, 2, 1, -7, 6});
- assertArrayScan(2, 2, new int[]{-5, -2, -1, -4, -7});
- assertArrayScan(0, 8, new int[]{4, 1, 1, 4, -4, 10, -4, 10, 3, -3, -9, -8, 2, -6, -6, -5, -1, -7, 7, 8});
- }
- private static void assertArrayScan(int startIndex, int endIndex, int[] array) {
- System.out.println("Original : " + Arrays.toString(array));
- int[] expected = Arrays.copyOfRange(array, startIndex, endIndex + 1);
- System.out.println("Expecting: " + Arrays.toString(expected));
- int[] actual = scanArray(array);
- System.out.println("Actual : " + Arrays.toString(actual));
- System.out.println();
- assertArrayEquals(expected, actual);
- }
- private static int[] scanArray(int[] array) {
- if (array == null || array.length == 0)
- throw new IllegalArgumentException("Array cannot be null and must contain at least one element");
- int maxStartingIndex = 0;
- int maxEndingIndex = 0;
- int max = array[0];
- outer:
- for (int i = 0; i < array.length; i++) {
- int value = array[i];
- if (value > max) {
- max = value;
- maxStartingIndex = i;
- maxEndingIndex = i;
- }
- if (value < 0)
- continue;
- System.out.println();
- System.out.println(String.format("Starting looping on %d, max is %d for indexes %d -- %d", i, max, maxStartingIndex, maxEndingIndex));
- boolean swapped = false;
- int currentSum = value;
- for (int j = i + 1; j < array.length; j++) {
- int jValue = array[j];
- boolean innerPositive = jValue >= 0;
- System.out.println(String.format("CurrentSum %d, i %d, j %d, swapped %s, innerPos %s, max is %d for index %d -- %d", currentSum, i, j, swapped, innerPositive, max, maxStartingIndex, maxEndingIndex));
- if (!swapped && innerPositive) {
- currentSum += jValue;
- if (currentSum >= max) {
- max = currentSum;
- maxStartingIndex = i;
- maxEndingIndex = j;
- }
- }
- else if (swapped && !innerPositive) {
- currentSum += jValue;
- }
- else if (!swapped && !innerPositive) {
- swapped = true;
- if (currentSum >= max) {
- max = currentSum;
- maxStartingIndex = i;
- maxEndingIndex = j - 1;
- }
- currentSum += jValue;
- }
- else { // swapped && innerPositive
- if (currentSum >= 0) {
- maxStartingIndex = i;
- currentSum += jValue;
- swapped = false;
- if (currentSum >= max) {
- maxEndingIndex = j;
- max = currentSum;
- System.out.println("New record: " + max + " from " + maxStartingIndex + " to " + maxEndingIndex);
- }
- }
- else {
- currentSum = 0;
- i = j - 1;
- break;
- }
- }
- if (j == array.length - 1) {
- break outer;
- }
- /**
- * // 6, -3, -2, 7
- * swapped innerPositive action
- * false true add to sum and continue
- * true false Add to current sum, continue
- * false false set swapped = true. Check current sum. Also continue.
- * true true Check if current sum >= 0. If it is, possibly save indexes and continue. Otherwise, reset current sum to zero and start over on the new index.
- */
- }
- }
- return Arrays.copyOfRange(array, maxStartingIndex, maxEndingIndex + 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement