Javatpoint Logo
Javatpoint Logo

Kadane's Algorithm in Java

Kadane's algorithm, named for computer scientist Jay Kadane, is one of the most important methods for determining the largest sum subarray of integers from an array. It was first presented in 1984. Due to its efficiency and simplicity, this algorithm is frequently used in a variety of fields, including data analysis, business, and computer science.

The problem Kadane's Algorithm addresses is fundamental: If we are given an array of integers, we should find the contiguous sub-array with the largest sum. This problem is of interest in diverse fields such as in stock market analysis, signal processing and image processing. The main idea behind Kadane's Algorithm is the cleverness of its dynamic programming technique.

The algorithm, by going through the array from left to right and applying scanning iteratively, efficiently finds the maximum sum subarray at each step. It achieves this by maintaining two crucial variables: maxEndingHere and maxEndingFar.

Kadane's Algorithm works by iteratively scanning the array from left to right, keeping track of two key variables:

currentSum: The current subarray being dealt with now is the sum of its elements. It's important since it determines if the current subarray is to be extended or a new subarray should be initiated from the current element.

maxSum: This container tracks a maximum sum seen so far. While going through the array algorithm proceeds with updating maxSum value when new maximum sum subarray is discovered.

Kadane's Algorithm

1. Initialize two variables, max_so_far and max_ending_here, to the first element of the array.

2. Iterate over the array from the second element to the end:

3. Calculate the maximum sum ending at the current position:

4. Update max_so_far to be the maximum of max_so_far and max_ending_here:

5. Return max_so_far as the maximum sum of any subarray in the array.

Implementation of Kadane's Algorithm

The algorithm is efficient since it uses past computations to compute the maximum sum subarray ending at every given position in the array thus hence there are no redundant calculations. It gets a time complexity O(n), where n is the number of elements in the array by iteration through this array only once.


Maximum contiguous sum is 6


The KadaneAlgorithm class has a method named maxSubarraySum() that determines the largest summation of a consecutive subarray in an array of integers that is provided as input. It uses Kadane's Algorithm that is dynamic programming approach.

The two variable, maxSoFar and maxEndingHere, are set to the first element of the array.

It goes through the array, setting maxEndingHere to that element or to the sum of that element and maxEndingHere. The maxSoFar is updated to the max of its current value and the max ending here, ensuring it holds the maximum sum found so far.

After looping, maxSoFar is assigned the maximum sum of a contiguous subarray, which is eventually returned.

The main() method demonstrates its usage: The example input array is there.

The maxSubArraySum() function is called with this array, which results in maximum sum, and it is printed on the console.

Complexity Analysis

Time Complexity

The algorithm traverses the input array once, processing each element in constant time. It calculates the maximum sum subarray efficiently by updating two variables (maxSoFar and maxEndingHere) at each iteration. The linear complexity of Kadane's Algorithm makes it highly efficient, especially for large input arrays, as it eliminates redundant computations and skips unnecessary iterations.

Space Complexity

Kadane's algorithm needs a constant amount of extra space no matter how large the input array is. It needs only two integer variables (maxSoFar and maxEndingHere) to be used for storing the maximal amount subarray and the maximal amount subarray ending at the current position. It leads to the fact that space complexity is unchanged which means that the algorithm is space-indifferent and is appropriate for memory-limited surroundings.


Kadane's Algorithm is an effective and efficient way of finding the maximum sum subarray from a given integer array. One iteration through the array, it effectively finds the contiguous subarray with the largest sum as it uses dynamic programming concept. Its time complexity of O(n), where n is the number of elements in the array, makes it very efficient even for the large datasets. Furthermore, the space complexity of Kadane's Algorithm is O(1), requiring just a constant amount of additional space regardless of input size.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA