-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay8.java
More file actions
28 lines (25 loc) · 866 Bytes
/
Copy pathDay8.java
File metadata and controls
28 lines (25 loc) · 866 Bytes
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
/*Kadane's Algorithm
You are given an integer array arr[]. You need to find the maximum sum of a subarray (containing at least one element) in the array arr[].
Note : A subarray is a continuous part of an array.
Examples:
Input: arr[] = [2, 3, -8, 7, -1, 2, 3]
Output: 11
Explanation: The subarray [7, -1, 2, 3] has the largest sum 11.
Input: arr[] = [-2, -4]
Output: -2
Explanation: The subarray [-2] has the largest sum -2.
Input: arr[] = [5, 4, 1, 7, 8]
Output: 25
Explanation: The subarray [5, 4, 1, 7, 8] has the largest sum 25. */
/*class Solution {
int maxSubarraySum(int[] arr) {
// Code here
int currentSum =arr[0];
int maxSum =arr[0];
for(int i=1;i<arr.length;i++){
currentSum =Math.max(arr[i],currentSum+arr[i]);
maxSum=Math.max(maxSum,currentSum);
}
return maxSum;
}
} */