Size of sub-array with max sum in C

Size of sub-array with max sum

Here, in this page we will discuss the program to find size of sub-array with max sum in C. We use Kadane’s algorithm, which runs in O(n) time.
The idea is to keep scanning through the array and calculating the maximum sub-array that ends at every position. If current sum becomes 0 then at that point we also update the starting index.

size of subarray with max sum

What is the Sub-array with Maximum Sum?

A sub-array is a contiguous part of an array. The maximum sub-array sum problem is to find the sub-array (containing at least one number) which has the largest sum.

The goal of this problem is to not only find the sum, but also to determine the size of this sub-array.

Size of sub-array with max sum in C

The size of the sub-array with the maximum sum in an array is the number of elements that are part of that sub-array.

For example, in the array

 [1, -2, 3, 5, -3, 2], the sub-array with the maximum sum is [3, 5], and its size is 2.

To find the size of the sub-array with the maximum sum in C, you can modify the Kadane’s algorithm implementation to keep track of the size of the sub-array as well.

Size of sub-array with max sum in C

Algorithm:

  1. Create two intermediate variables max_ending_here  and max_so_far .

  2. And also two variables start and end for tracking start and the end index of the subarray with maximum sum.

  3. Initialized these two intermediate variables using the 0.

  4. Traverse the array from 0 to N-1 and calculate the max_ending_here and max_so_far.

  5. Now we will keep max_so_far which indicates the maximum sum found so far.

  6. During the scanning of  the elements from starting, and when the max_ending_here becomes 0 at that point we also update start and end variables.

Kadane’s Algorithm

Kadane’s algorithm is used to solve this problem efficiently by scanning the array once and maintaining a running sum.

Key Variables Used:

  • max_ending_here: Maximum sum of sub-array ending at the current index
  • max_so_far: Maximum sum so far
  • start, end: Indices of the sub-array with the maximum sum
  • s: Temporary starting index

How the Algorithm Works:

  • Initialize all variables to 0.
  • Traverse through the array.
  • Add the current element to max_ending_here.
  • If max_ending_here becomes greater than max_so_far, update max_so_far and note the start and end indices.
  • If max_ending_here becomes negative, reset it to 0 and update the temporary start index s.

C code for Size of sub-array with max sum

Run
#include<stdio.h>

int maxSubArraySum(int a[], int size)
{
    int max_so_far = 0, max_ending_here = 0,
       start =0, end = 0, s=0;
 
    for (int i=0; i< size; i++ )
    {
        max_ending_here += a[i];
 
        if (max_so_far < max_ending_here)
        {
            max_so_far = max_ending_here;
            start = s;
            end = i;
        }
 
        if (max_ending_here < 0)
        {
            max_ending_here = 0;
            s = i + 1;
        }
    }
     
    return (end - start + 1);
}
 
int main()
{
    int a[] = {-7, 3, 4, -1, -12, 1, -9, -3};
    int n = sizeof(a)/sizeof(a[0]);
    printf("Size of subarray with maximum sum is : %d",maxSubArraySum(a, n));
    return 0;
}

Output

Size of subarray with maximum sum is : 2

 Time and Space Complexity

  • Time Complexity: O(n) — we go through the array only once.
  • Space Complexity: O(1) — only a fixed number of variables are used.

Method:2

Run
#include

int maxSubArraySize(int a[], int n) {
int max_sum = a[0];
int size = 1;

for (int i = 0; i < n; i++) {
int current_sum = 0;
for (int j = i; j < n; j++) { current_sum += a[j]; if (current_sum > max_sum) {
max_sum = current_sum;
size = j - i + 1;
}
}
}
return size;
}

int main() {
int a[] = {-2, -3, 4, -1, -2, 1, 5, -3};
int n = sizeof(a) / sizeof(a[0]);
printf("Size of subarray with maximum sum is: %d", maxSubArraySize(a, n));
return 0;
}

Output

Size of subarray with maximum sum is: 5

 Time and Space Complexity

  • Time Complexity: O(n²) — Two nested loops check all sub-arrays.

  • Space Complexity: O(1) — No extra space used.

Closing Thoughts

Understanding how to find the size of the sub array with the maximum sum is crucial for mastering array based problems in C. By using Kadane’s Algorithm, we can efficiently solve this problem in linear time with constant space, making it suitable for large data sets.

This approach not only helps in identifying the maximum sum but also tracks the exact size of the sub array that contributes to this maximum sum. Mastery of this logic is essential for optimizing performance in technical interviews and competitive coding.

FAQs

Kadane’s Algorithm helps find the sub array with the maximum sum in linear time by maintaining a running sum and updating indices when a higher sum is found.

Track the starting and ending indices of the sub-array when the maximum sum is updated, then calculate size as end – start + 1.

Yes, the sub-array can contain negative numbers as long as the overall sum is the highest possible among all sub-arrays.

The time complexity is O(n) as we traverse the array once, and the space complexity is O(1) because only a few variables are used.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription