-
Notifications
You must be signed in to change notification settings - Fork 0
/
LargestSum.java
32 lines (29 loc) · 961 Bytes
/
LargestSum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package code.datastructures.arrays;
public class LargestSum {
/**
* This function finds the maximum sum present inside a contiguous sub array. Time complexity is
* O(n) and space complexity is O(1).
* Level - Medium.
*/
public int largestContiguousSubArray(int[] arr) {
int currentMax = arr[0], totalToReturn = arr[0];
for (int i = 1; i < arr.length; i++) {
currentMax = Math.max(arr[i], currentMax + arr[i]);
totalToReturn = Math.max(currentMax, totalToReturn);
}
return totalToReturn;
}
/**
* This is the Kadane's Algorithm. Help to get the sum of the longest subarray in O(n) time
* complexity and O(1) space complexity.
* Level - Medium
*/
public int kadanesAlgorithm(int[] arr) {
int currentMax = arr[0], max = arr[0];
for (int i = 1; i < arr.length; i++) {
currentMax = Math.max(arr[i], currentMax + arr[i]);
max = Math.max(currentMax, max);
}
return max;
}
}