
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Implement Kadane's Algorithm in 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. In this article, we are going to implement Kadane's algorithm using C.
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 Largest Sum Contiguous Subarray Problem in reduced time complexity of O(N) using a dynamic programming approach. Kadane's algorithm was given by Jayadev Misra and Mahesh K. Kadane in 1984.
Example 1
- Input: arr = {-2, 1, -3, 4, -1, 2, 1, -5, 4}
- Output: Maximum Subarray Sum: 6
Explanation:
The contiguous subarray that has the maximum sum is [4, -1, 2, 1].The maximum sum is 6.
Example 1
- Input: arr = { 1, 2, 3, 4, 5, 6, 7 }
- Output: Maximum Subarray Sum: 28
Explanation:
The contiguous subarray that has the maximum sum is an entire array itself [1, 2, 3, 4, 5, 6, 7].
The maximum sum is 28
Approaches to finding the Maximum Subarray Sum using Kadane's Algorithm
To solve this problem, here are the two approaches that you can use:
- Kadane's Algorithm
- Optimized Kadane's Algorithm (Handles all negative elements)
Iterative Kadane's Algorithm
This is a simple approach to finding the maximum sum of a contiguous subarray. Using this algorithm we traverse the array once while keeping two variables. One variable current_sum is used to keep track of the sum of the current subarray and another variable max_sum is used to store the maximum subarray sum encountered so far. If the current sum becomes negative make current_sum 0 because a negative sum will reduce the overall subarray sum.
Steps for Implementation
- Create two variables current_sum = 0 and max_sum = INT_MIN.
- 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 with current_sum.
- If current_sum becomes negative, reset it to 0.
- After iterating the array, return the max_sum.
Implementation Code
#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("Maximum Subarray Sum: %d", kadane(arr, n)); return 0; }
Output
Maximum Subarray Sum: 6
Time Complexity: O(N)
Space Complexity: O(1)
Optimized Kadane's Algorithm (Handles all negative elements)
In the above Kadane's algorithm, we have seen that current_sum resets to 0 when it becomes negative. However, in any case, if all elements in array are negative, it will always return 0, which is incorrect. To deal with such edge cases, we modify the algorithm to track the maximum element.
Implementation Code
#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("Maximum Subarray Sum: %d", kadaneOptimized(arr, n)); return 0; }
Output:
Maximum Subarray Sum: -1
Time Complexity: O(n)
Space Complexity: O(1)