title: Maximum Contiguous Subsequence Sum and sequence
body: <p>I am trying to find to the max sum of an array and print the corresponding sequence that produces that max sum. I have been able to get the correct sum but when I try to print the sequence for some of the test arrays my program leaves off one of the indices. For example, for the array [1, -1, 2, 3, -2] my program finds the max sum of 5 but it only prints 1, -1, 2 instead of 1, -1, 2, 3. I know the problem is inside my for loop and my count variable not incrementing correctly but I do not know how to fix it. </p>
  
  <pre><code>    import java.util.*;
  
      public class practice
      {
          public static void main(String args[])
          {
              int arr[] = {1, -1, 2, 3, -2};
              int arr2[] = {1, 12, -2, -15, 10};
              int arr3[] = {0, -1, -3, -5, -6};
              int arr4[] = {1, 2, 3, 4, 5};
              int arr5[] = {1, 12, -2, 15, 10};
  
              subsequence(arr);
              subsequence(arr2);
              subsequence(arr3);
              subsequence(arr4);
              subsequence(arr5);
  
          }
  
          public static void subsequence(int[] arr)
          {
              int max = 0;
              int tempMax = 0;
              int count = 0;
  
              // My problem is in here:
              for (int i = 0; i &lt; arr.length; i++)
              {
                  tempMax += arr[i];
                  if (max &lt; tempMax)
                  {
                      max = tempMax;
                      count++;
                  }
              }
  
              System.out.println("count = " + count);
              System.out.println("Max sum is " + max);
              System.out.print("Sequence is: ");
  
              for (int j = 0; j &lt; count; j++)
                  System.out.print(arr[j] + " ");
  
              System.out.println("\n");
          }
      }
  </code></pre>
  
  <p>here is my output</p>
  
  <pre><code>    count = 3
      Max sum is 5
      Sequence is: 1 -1 2 
  
      count = 2
      Max sum is 13
      Sequence is: 1 12 
  
      count = 0
      Max sum is 0
      Sequence is: 
  
      count = 5
      Max sum is 15
      Sequence is: 1 2 3 4 5 
  
      count = 4
      Max sum is 36
      Sequence is: 1 12 -2 15 
  </code></pre>
  
  <p>here is my edited code:</p>
  
  <pre><code>public class practice
  {
      public static void main(String args[])
      {
          int arr[] = {1, -1, 2, 3, -2};
          int arr2[] = {1, 12, -2, -15, 10};
          int arr3[] = {0, -1, -3, -5, -6};
          int arr4[] = {-1, 2, 3, -4, -5};
          int arr5[] = {1, 12, -2, 15, 10};
  
          subsequence(arr);
  
          subsequence(arr2);
  
          subsequence(arr3);
  
          subsequence(arr4);
  
          subsequence(arr5);
      }
  
      public static void subsequence(int[] arr)
      {
          int max = 0;
          int tempMax = 0;
          int count = 0;
          int start = 0;
          int end = 0;
  
          if (arr[0] &lt; 0)
             start++;
  
  
          for (int i = start; i &lt; arr.length; i++)
          {
              tempMax += arr[i];
  
              if (max &lt; tempMax)
              {
                  max = tempMax;
                  count = i;
              }
  
              if (Math.abs(arr[i]) &lt; tempMax)
                 end = i;
  
  
           }
  
           System.out.println("count = " + count);
           System.out.println("Max sum is " + max);
           System.out.print("Sequence is: ");
  
           if (arr[end] &lt; 0)
                 end--;
  
           for (int j = start; j &lt;= end; j++)
               System.out.print(arr[j] + " ");
  
           System.out.println("\n");
           }
  
      }
  </code></pre>
  
  <p>and here is my new output:</p>
  
  <pre><code>count = 3
  Max sum is 5
  Sequence is: 1 -1 2 3 
  
  count = 1
  Max sum is 13
  Sequence is: 1 12 
  
  count = 0
  Max sum is 0
  Sequence is: 0 
  
  count = 2
  Max sum is 5
  Sequence is: 2 3 
  
  count = 4
  Max sum is 36
  Sequence is: 1 12 -2 15 10 
  </code></pre>