C program to Implement Kadane’s Algorithm

C

We are given an array of integers, and we need to find the maximum sum of a contiguous subarray using Kadane's Algorithm. Kadane's Algorithm is an efficient way to find the maximum subarray sum in O(n) time complexity. For example, in the array {-2, 1, -3, 4, -1, 2, 1, -5, 4}, the subarray [4, -1, 2, 1] has the maximum sum 6.

What Is Kadane's Algorithm?

Kadane's algorithm is a popular and optimal algorithm used to find the maximum sum of a contiguous subarray in a given array of integers. This algorithm efficiently solves the Maximum Subarray Problem in O(n) time complexity using a dynamic programming approach. It was developed by Jay Kadane in 1984.

Syntax

int kadane(int arr[], int n);

Algorithm Steps

  • Initialize max_sum to the first element and current_sum to 0
  • Iterate through each element of the array
  • Add the current element to current_sum
  • If current_sum is greater than max_sum, update max_sum
  • If current_sum becomes negative, reset it to 0
  • Return max_sum as the result

Method 1: Basic Kadane's Algorithm

This approach works well for arrays that contain at least one positive number

#include <stdio.h>

int kadane(int arr[], int n) {
    int max_sum = arr[0];
    int current_sum = 0;

    for (int i = 0; i < n; i++) {
        current_sum += arr[i];
        if (current_sum > max_sum) {
            max_sum = current_sum;
        }
        if (current_sum < 0) {
            current_sum = 0;
        }
    }
    return max_sum;
}

int main() {
    int arr[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("Array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\nMaximum Subarray Sum: %d<br>", kadane(arr, n));
    return 0;
}
Array: -2 1 -3 4 -1 2 1 -5 4 
Maximum Subarray Sum: 6

Method 2: Optimized Kadane's Algorithm (Handles All Negative Elements)

The basic version fails when all elements are negative. This optimized version handles such cases correctly

#include <stdio.h>

int kadaneOptimized(int arr[], int n) {
    int max_sum = arr[0];
    int current_sum = arr[0];

    for (int i = 1; i < n; i++) {
        current_sum = (current_sum > 0) ? (current_sum + arr[i]) : arr[i];
        if (current_sum > max_sum) {
            max_sum = current_sum;
        }
    }
    return max_sum;
}

int main() {
    int arr[] = {-5, -3, -1, -2, -4};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("Array with all negative elements: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\nMaximum Subarray Sum: %d<br>", kadaneOptimized(arr, n));
    return 0;
}
Array with all negative elements: -5 -3 -1 -2 -4 
Maximum Subarray Sum: -1

Comparison

Method Handles All Negatives Time Complexity Space Complexity
Basic Kadane's No O(n) O(1)
Optimized Kadane's Yes O(n) O(1)

Key Points

  • Kadane's algorithm uses dynamic programming to solve the maximum subarray problem efficiently
  • The basic version may return 0 for arrays with all negative elements
  • The optimized version correctly handles edge cases including all-negative arrays
  • Both versions have linear time complexity and constant space complexity

Conclusion

Kadane's Algorithm provides an optimal solution for finding the maximum subarray sum with O(n) time complexity. The optimized version should be preferred as it handles all edge cases correctly, including arrays with all negative elements.

Akansha Kumari
Akansha Kumari

Hi, I am Akansha, a Technical Content Engineer with a passion for simplifying complex tech concepts.

Updated on: 2026-03-15T14:34:52+05:30

512 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements