Maximum subarray problem solved with divide and conquer also for the all negative element case Solving the maximum subarray problem with added cases of finding non maximum non contiguous subarray as well. I used divide and conquer approach and tackled the 'all negative' case by keeping a counter for that and branching it.  

#include&lt;stdio.h&gt;
#include&lt;limits.h&gt;
#include&lt;stdlib.h&gt;

// finding maximum non contiguous  subarray by adding up all the non negative elements in array

int max_noncont_sub(int a[],int *lower, int *upper)
{
    int sum=0, i ;
for(i = *lower; i &lt;= *upper; i++)
{
    if(a[i]&gt;0)
        sum += a[i];    
}
return sum;
}

// finding max subarray which include the middle term, starting from mid and branching both sides till we encounter negative element

int max_cross_sub(int A[], int *lower, int mid, int *upper)
{
int left_sum = INT_MIN;
int sum = 0;
int i, max_left;

for(i = mid; i&gt;=*lower; i--)
{
    sum = sum + A[i];
    if(sum&gt;left_sum)
        left_sum = sum;
    max_left = i;
}

int right_sum = INT_MIN;
sum = 0;
int max_right;

for(i = mid+1; i&lt;=*upper; i++)
{
    sum = sum + A[i];
    if(sum&gt;right_sum)
        right_sum = sum;
    max_right = i;
}
*lower = max_left;
*upper = max_right;
return left_sum + right_sum;

}

//dividing the problem into 3 parts, left subarray and right have same substructure, 3rd one is solved by max_noncont_sub().

int max_sub(int A[], int *lower, int *upper)
{
    if(*lower == *upper)
    {
        return A[*lower];
    }   else
          {
               int mid = *lower + (*upper - *lower)/2;
               int left_low = *lower;
               int left_high = mid;
               int left_sum = max_sub(A, &amp;left_low, &amp;left_high);

               int right_low = mid+1;
               int right_high = *upper;
               int right_sum = max_sub(A, &amp;right_low, &amp;right_high);

               int cross_low = *lower;
               int cross_high = *upper;
               int cross_sum = max_cross_sub(A, &amp;cross_low, mid, &amp;cross_high);

               if(left_sum &gt;=right_sum &amp;&amp; left_sum&gt;= cross_sum)
               {
                   *lower = left_low;
                    *upper = left_high;
                  return left_sum;
               } else if(right_sum &gt;=left_sum &amp;&amp; right_sum&gt;= cross_sum)
                   {
                        *lower = right_low;
                        *upper = right_high;
                        return right_sum;
                   } else 
                       {
                            return cross_sum;
                        }


        }
 }

int main()
{
    int T, N, nc=0;
     scanf("%d",&amp;T);
    while(T--)
    {
        scanf("%d",&amp;N);
        int *a = (int*)malloc(N*sizeof(int));
        int i;

        nc=0;
        for(i = 0; i&lt;N; i++)
       {    
            scanf("%d",&amp;a[i]);
            if(a[i]&lt;0)
                nc++; // counting the negative numbers for later tackling with all negative special case
       }

    int lower = 0;
    int upper = N-1;

    if(nc == N)  // if all elements are negative, we just get the least negative and its the answer.
    {
        int max=INT_MIN;
        for(i =0;i&lt;N;i++)
        {
            if(a[i]&gt;max)
                max = a[i];
        }
        printf("%d %d\n",max, max);

    }
    else
    {
        printf("%d %d\n",max_sub(a, &amp;lower, &amp;upper), max_noncont_sub(a, &amp;lower, &amp;upper));
    }
 }
   return 0;
}


Any review and suggestions are welcome.
Is there a better way to do it? I know there's kadane's algorithm with O(n) running time.
Bugs


Your sum in max_cross_sub() could overflow an int, causing a change in sign.  Therefore, you could either miss the maximum subarray if your sum erroneously became negative, or get the wrong subarray if your sum erroneously became positive.
It seems that you want lower and upper to return the bounds of the maximum subarray.  Otherwise you could just pass those arguments as regular integers instead of pointers.  However, max_sub() doesn't set the correct bounds in the case of cross_sum being the max sum.
In addition to #2, max_cross_sub() doesn't set *lower and *upper to the correct bounds.  The problem is that you don't set max_left and max_right correctly, so you end up leaving the bounds unchanged from whatever was passed in.
This line isn't right:

printf("%d %d\n",max_sub(a, &amp;lower, &amp;upper), max_noncont_sub(a, &amp;lower, &amp;upper));


The problem with it is that the call to max_sub() changes lower and upper, so that the call to max_noncont_sub() has the wrong bounds passed in.  You should use two separate copies of the lower and upper bounds instead.


Better way

Clearly you already knew about Kadane's algorithm, because you mentioned it.  The question is why you didn't use it?  Not only is it faster, it's also only 6 lines of code compared to the roughly 60 lines you used to do it the divide and conquer way.