Note that there are some explanatory texts on larger screens.

plurals
  1. POSub-Array Max Sum
    primarykey
    data
    text
    <p>I'm looking over an assignment that I finished a few days ago and realized I'm not supposed to use constants. The assignment is the well-known "find the largest sum of a sub-array of integers both positive and negative recursively using a divide and conquer approach" problem. My algorithm works, but a part of it uses a constant in order to figure out the largest sum of sub-arrays that include the middle of the array. </p> <p>Here's the relevant code:</p> <pre><code>lfSum = Integer.MIN_VALUE; sum = 0; // Sum from left to mid for (int i = mid; i &gt;= LF; i--) { sum += array[i]; if (sum &gt; lfSum) { lfSum = sum; if (lfSum &gt; lfMax) { lfMax = lfSum; } } } rtSum = Integer.MIN_VALUE; sum = 0; // Sum from mid to right for (int j = mid+1; j &lt;= RT; j++) { sum += array[j]; if (sum &gt; rtSum) { rtSum = sum; if (rtSum &gt; rtMax) { rtMax = rtSum; } } } // Largest sum spanning whole array midMax = lfSum + rtSum; // midMax = leftMid + midRight; </code></pre> <p>What this does is it loops through each half of the entire array and checks to see if the sum is larger than the smallest integer possible in case the entire array is negative. If it is, it sets that side's max sum to sum's value. If that value is larger than what one of the recursive calls returned (lfMax or rtMax), set the respective side's recursive value to it.</p> <p>Like I said earlier, this works perfectly well, but I'm not supposed to be using "Integer.MIN_VALUE". Is there another way around this? Obviously I could initialize lfSum/rtSum to the numerical value of Integer.MIN_VALUE, but I'd like to know if there are any other options.</p> <p>I've tried removing rtSum/lfSum and just comparing sum to the recursive values, and initializing lfSum/rtSum to 0, but both did not work correctly. Thanks for taking the time to read this!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload